Multiple Inheritance - does order matter?

M

Mark

When declaring a class that uses multiple inheritance, does the order used
when listing the inheritance matter? I'm finding with my compiler (gcc
3.2.2) that my program seg faults when destructing if the order is "wrong".

In my program I use an STL vector to store objects of type Server *. Server
is an abstract base class. When exiting my program I iterate through the
vector and call delete on all my Server objects. One of the concrete Server
objects inherits from both Server and a class called Process. If I declare
the class as:

class MyServer : public Server, public Process
{
};

it's fine, but if I declare it as:
class MyServer : public Process, public Server
{
};

my program seg faults when trying to call the destructor.

Can anyone give me a hint as to what I'm doing wrong?

Thanks,
Mark
 
V

Victor Bazarov

Mark said:
When declaring a class that uses multiple inheritance, does the order used
when listing the inheritance matter? I'm finding with my compiler (gcc
3.2.2) that my program seg faults when destructing if the order is "wrong".

In my program I use an STL vector to store objects of type Server *. Server
is an abstract base class. When exiting my program I iterate through the
vector and call delete on all my Server objects. One of the concrete Server
objects inherits from both Server and a class called Process. If I declare
the class as:

class MyServer : public Server, public Process
{
};

it's fine, but if I declare it as:
class MyServer : public Process, public Server
{
};

my program seg faults when trying to call the destructor.

Can anyone give me a hint as to what I'm doing wrong?

You probably forgot to declare 'Server's destructor virtual.

Victor
 
M

Mark

Hi Victor, that's what I thought too but all destructors are already
virtual. Maybe something else is going on ...

Mark
 
V

Victor Bazarov

Mark said:
Hi Victor, that's what I thought too but all destructors are already
virtual. Maybe something else is going on ...

Reduce your program to the bare minimum that still exhibits the error you
posted about and then post the code, we could try to analyse it further.

Of course, if it has too much OS-dependent code (forking, threading, or
whatever) and removing it stops the error from re-occurring, it probably
is not a language problem...

And, please, don't top-post.
 
D

David Harmon

When declaring a class that uses multiple inheritance, does the order used
when listing the inheritance matter?

Your base classes are constructed in the order in which they are
declared, and destructed in the reverse order. (The order in which they
are mentioned in a constructor initializer list is irrelevant.) Other
than that I don't think it should make any difference.
 
M

Mark

Your base classes are constructed in the order in which they are
declared, and destructed in the reverse order. (The order in which they
are mentioned in a constructor initializer list is irrelevant.) Other
than that I don't think it should make any difference.

Dumb error, I was too quick to dismiss the non-virtual destructor issue.
One of my abstract base classes didn't have a destructor declared at all,
just a few pure virtual methods that must be implemented. So when I added
a trivial destructor it worked.

Thanks!
Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top