Virtual D'tor

Started by
1 comment, last by The C modest god 18 years, 8 months ago
If I have the following code:

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?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Advertisement
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
Thanks a lot.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.

This topic is closed to new replies.

Advertisement