Note that there are some explanatory texts on larger screens.

plurals
  1. POconst object& return value from a method, best practice
    primarykey
    data
    text
    <p>My C++ knowledge are minimal, for this i'm asking this:</p> <p>I have a pointer to an object of type <code>Ndb</code> and i do this:</p> <pre><code>Ndb* myObj=new Ndb(); NdbError err=myObj-&gt;getNdbError(); //Do some work; //Finish the job delete(myObj); </code></pre> <p>The method's signature for <code>getNdbError</code> is:</p> <p><code>const NdbError&amp; getNdbError() const</code></p> <p>According to the signature, who must take care of free the memory for the <code>NdbError</code>? the <code>const</code> before <code>NdbError&amp;</code> means that i can only read the result and i should not to anything else? I don't wont to leave some memory area allocated but not referenced.</p> <p>EDIT: According to the ansers its better to show more of this use case. I have a wrapper beetween a C++ object and C methods:</p> <pre><code>void* WINAPI new_Ndb(void* cluster_connection,const char* catalogname,const char* schemaName) { Ndb_cluster_connection* co=(Ndb_cluster_connection*)cluster_connection; Ndb* tmpN=new Ndb(co,catalogname,schemaName); return (void*)tmpN; } void WINAPI Ndb_dispose(void* obj) { delete (Ndb*)obj; obj=0; } const ndberror_struct WINAPI Ndb_getNdbError(void* obj) { Ndb* tmp=(Ndb*)obj; NdbError res=tmp-&gt;getNdbError(); ndberror_struct tmpRes; tmpRes=(ndberror_struct)res; return tmpRes; } </code></pre> <p>NdbError define an operator overloading:</p> <pre><code> operator ndberror_struct() const { ndberror_struct ndberror; ndberror.status = (ndberror_status_enum) status; ndberror.classification = (ndberror_classification_enum) classification; ndberror.code = code; ndberror.mysql_code = mysql_code; ndberror.message = message; ndberror.details = details; return ndberror; } </code></pre> <p>What will appen when i make a call to</p> <p><code>const ndberror_struct WINAPI Ndb_getNdbError(void* obj)?</code></p> <p>The Ndberror will go out of scope but i keep the data because are copied? <code>message</code> and <code>details</code> are <code>char*</code> so they will be freed when the return value ndb_error_struct will go out of scope?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload