I am alittle bit confused on topic of allocating objects on heap vs allocating on stack, and when and how delete() should be called.
For example I have class Vector.
delete [] v;
- it's an array notation of delete operator. You have to use it when deleting arrays, instead of deleting each element sequentially.Vector* v = Vector[100];
just won't compile. Write Vector v[100];
and it will allocate array of vectors on stack. You may not delete it manually.x, y, z
are on the heap as the whole object is on the heapitems[10]
is also allocated on the heap as is makes part of the object. To delete it just call delete v2;
. You don't need a special destructor unless you allocate anything in constructor.int* items;
is stored on the stack as it's part of the object allocated on stack. You don't have to delete it, it will be deleted automatically when comes out of scope.