Step 3: C++ Smart Pointers

Sometimes it is possible and beneficial from a performance standpoint to access object fields in a more standard С++ fashion, using setters and getters as opposed to the generated wrapper methods which call the underlying C API classname_fieldname_put() and classname_fieldname_get() functions. These setters and getters make database fields appear effectively as class properties. For example, instead using generated methods like:

 
    anObject.aField_get(&value));
    anObject.aField_put(1));
 

the mcocomp option –smartptr can be used to generate "smart pointers" that allow the above classname_fieldname_get() and classname_fieldname_put() operation to be written as:

 
    value = anObject.aField;
    anObject.aField = 1;
     

In order to use smart pointer access, simply specify the -smartptr option as follows:

 
    ../../host/bin/mcocomp -hpp -smartptr schema.mco
     

The sample samples/native/core/20-languages/cpp/smartptr demonstrates the use of smart pointers using the same database as the previous step. Note how the function populate_db() differs from the cppdemo sample.