I\'m coming from a Java background and have started working with objects in C++. But one thing that occurred to me is that people often use pointers to objects rather than t
In C++, objects allocated on the stack (using Object object;
statement within a block) will only live within the scope they are declared in. When the block of code finishes execution, the object declared are destroyed.
Whereas if you allocate memory on heap, using Object* obj = new Object()
, they continue to live in heap until you call delete obj
.
I would create an object on heap when I like to use the object not only in the block of code which declared/allocated it.