C++: Creating a local copy of an Object

891 Views Asked by At

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){}
2

There are 2 best solutions below

5
On BEST ANSWER

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)
3
On

assuming that GetLastError returns a pointer

if (conn->GetLastError() != nullptr)
{
  ULError error = *(conn->GetLastError());
  // use the error
}