[C++] Delete base class pointer which actually holds a derived class pointer?

Started by
10 comments, last by ToohrVyk 15 years, 8 months ago
Sorry to say, but what else are you doing then filling the pointer (and possibly allocating data) here?

int *pointer; // random value, w/e was in therepointer = new int(5); // I FILL the pointer with a memory address pointing to an interger with the value 5delete pointer;pointer = NULL; // I fill the pointer with w/e NULL holds


I can't think of a better name really...

Quote:Original post by CaspianB
You can enter 1 or 2. At compile time, the code doesn't know which variable obj will be allocated to when it calls delete. Nor does it care. It's handled at run time. This is even the case if you don't use virtual methods. However, to ensure your destructor is run for your derived class (and any other methods you may choose to override in the derived class that are in the base class), you need to be virtual.


Ah, I see your point, but how does it safe that information at run-time? At a special memory place?
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Advertisement
Quote:Original post by Decrius
I can't think of a better name really...
The technical and commonly used term is 'assign' (from 'assignment'). You assign the newly created lvalue to the pointer. The term 'fill' usually has another meaning (see, for instance, std::fill) which relates to filling a sequence with copies of an object.

Quote:Ah, I see your point, but how does it safe that information at run-time? At a special memory place?


Yes, it does. Where the data is saved depends on the compiler and the compiler settings (for instance, it can be stored as part of a virtual table).

This topic is closed to new replies.

Advertisement