Jump to

v06-l3-surviveScope

What keyword allows any object instantiated in a scope to survive out of this scope ?

???

Indeed, new allocates any variable on the heap, which means that those objects are not deallocated at the end of scope (except if specifically done with the delete keyword).

In contrast, the return statement does not assert systematically that an object created locally is preserved, for instance :

double& createLocal() {
    double value = 1.0;
    return value;
}

createLocal() returns a reference to value that is instantiated in the function code, but it does not make the variable survive it. Depending on the compiler, using this function can produce warning, segmentation fault or undefined behaviors.