class A {
public:
virtual ~A();
};
class B: public A {
public:
string str;
};
A * pointer;
pointer = new B;
delete pointer;
On the last line, will the D'tor of str will be called?
Virtual D'tor
If I have the following code:
Short answer: Yes.
Longer answer: Like any other virtual function a virtual destructor means that the function called is determined at runtime based on the dynamic type of the pointed at object. Since pointer points to a B and A's destructor is virtual the expression delete pointer; will cause B's destructor to be called. B's destructor will then destroy its member objects and then its base classes, in this case destroying str and then its A base class part.
Enigma
Longer answer: Like any other virtual function a virtual destructor means that the function called is determined at runtime based on the dynamic type of the pointed at object. Since pointer points to a B and A's destructor is virtual the expression delete pointer; will cause B's destructor to be called. B's destructor will then destroy its member objects and then its base classes, in this case destroying str and then its A base class part.
Enigma
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement