Search code examples
c++c++11sqlanywhereultralite

C++: Creating a local copy of an Object


I'm an objective-C developer that's struggling with C++ code.

Below is the code I have in C++:

ULConnection *conn;
...... //code that builds up the connection
conn->GetLastError()->GetString(value, 255);

I need to create a local copy (not reference) of GetLastError().

How do I get a local reference and also check for null?

Here is my failed attempt:

ULError error = *conn->GetLastError();
if (&error != NULL){}

Solution

  • As per my understanding function conn->GetLastError() returns a pointer of ULError and need to check is the return pointer is null or not.

    This will work for you.

    const ULError *error = conn->GetLastError();
    if (error){}
    

    Since C++11 you can do as follows instead comparing with NULL

    if( error != nullptr)