• Advertisement
Sign in to follow this  

[C++] Multiple inheritance and delete

This topic is 3965 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

I realized that I don't know the specific rules for instanciating a derived class with new, and deleting it through a pointer to a base class. Is the following well-behaved, and why?
class FirstBase { public: virtual ~FirstBase(); };
class SecondBase { public: virtual ~SecondBase(); };

class Derived: public FirstBase, public SecondBase {};

int main()
{
  SecondBase *b = new Derived;
  delete b;
}

Share this post


Link to post
Share on other sites
Advertisement
I'd say, "Why not?". Public inheritance means is-a, so Derived is-a FirstBase and also Derived is-a SecondBase. Either is a viable upcast.

Share this post


Link to post
Share on other sites

#include <iostream>

using std::cout;

class FirstBase { public: virtual ~FirstBase() {cout << "FB\n"; } };
class SecondBase { public: virtual ~SecondBase() {cout << "SB\n";} };

class Derived: public FirstBase, public SecondBase {};

int main()
{
SecondBase *b = new Derived;
delete b;
}



Results in:
SB
FB

on Mac OS X with gcc 4.0.1

Share this post


Link to post
Share on other sites
Apears to be covered in 25.15

http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.15

Seems to be somewhat more complicated with virtual inheritance, but apparently it's deterministic.

Share this post


Link to post
Share on other sites
So, my understanding is that delete actually changes (moves back by a few bytes) the pointer to be deleted based on the called destructor? Swanky.

Share this post


Link to post
Share on other sites
Quote:
Original post by dragongame
*** Source Snippet Removed ***

Results in:
SB
FB

on Mac OS X with gcc 4.0.1


So if it was this:

class FirstBase { public: virtual ~FirstBase() {cout << "FB\n"; } };
class SecondBase { public: virtual ~SecondBase() {cout << "SB\n";} };

class Derived: public FirstBase, public SecondBase {};

int main()
{
FirstBase *b = new Derived;
delete b;
}


It would result in:
FB
SB
? (I don't have my compiler with me)

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
Quote:
Original post by dragongame
*** Source Snippet Removed ***

Results in:
SB
FB

on Mac OS X with gcc 4.0.1


So if it was this:
*** Source Snippet Removed ***

It would result in:
FB
SB
? (I don't have my compiler with me)


SB
FB

on Visual C++ 2003

Share this post


Link to post
Share on other sites
Alpha_ProgDes, as I understand it, it is the order in which the classes are specified, not the type of the base pointer one is initializing, or providing to 'delete'. I am sure it is a lot easier for compilers this way, and also a lot more deterministic for everyone in general.

int main()
{
SecondBase *b = new Derived;
FirstBase* fb = reinterpret_cast<FirstBase*> (b); // This doesn't change anything
delete fb;
}




With the order in which ToohrVyk has specified the classes, the order of construction is FirstBase, SecondBase, and then Derived. As always, the order of destruction is the reverse of construction. Hence the appearance of 'SB' before 'FB'. And of course, the virtual destructors in the base classes ensure this proper calling of destuctors.

This results in the destruction order Derived, FirstBase, SecondBase:

class Derived : public SecondBase, public FirstBase { ...


Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
I wonder what determines the order.....?


The ordering is explained in Antheus's FAQ url, linkized here for convenience: Clicky

Share this post


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

  • Advertisement