Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

Virtual D'tor

This topic is 4685 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!