Multiple inheritance; destructors

This is a discussion on Multiple inheritance; destructors within the C++ Programming forums, part of the General Programming Boards category; If I have two base classes, GameObject and DrawableObject, and an object that inherits from both of these classes, like ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    125

    Multiple inheritance; destructors

    If I have two base classes, GameObject and DrawableObject, and an object that inherits from both of these classes, like
    class Thing : public GameObject, public DrawableObject
    and I keep a pointer to an instance of Thing as a pointer to a GameObject like so:
    GameObject *agameobject = new Thing;
    If I delete it later on, calling delete on that agameobject pointer, will the DrawableObject destructor be called for the Thing, even though you can't tell by the pointer that the thing being pointed to is a DrawableObject? (all of the destructors are virtual)

    Thanks in advance.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    As long as the destructor in the base classes is virtual, the base destructors will be called independently of the static and dynamic types of your pointer being different.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Cool, thanks.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    Just for clarification...

    What you must ensure is that dynamic binding is present. Under it, it will be the dynamic type of your pointer what will define the destructors being called. In your example, the static type of agameobject is GameObject*. However, the dynamic type is Thing*. As long as the base classes have their destructors defined as virtual, you will ensure dynamic binding. As such, Thing* is what is recognized as the type of your object.

    Dynamic Binding is the coolest thing!
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Multiple inheritance in C#
    By DavidP in forum C# Programming
    Replies: 1
    Last Post: 06-27-2008, 04:41 PM
  3. Multiple inheritance problem
    By Magos in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2006, 08:27 AM
  4. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  5. Multiple virtual inheritance
    By kitten in forum C++ Programming
    Replies: 3
    Last Post: 08-10-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21