473,856 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple Inheritance - does order matter?

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
Jul 22 '05 #1
5 2687
"Mark" <ma************ *@excite.com> wrote...
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
Jul 22 '05 #2
Hi Victor, that's what I thought too but all destructors are already
virtual. Maybe something else is going on ...

Mark

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:FYIKb.7615 79$Tr4.2169809@ attbi_s03...
"Mark" <ma************ *@excite.com> wrote...
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

Jul 22 '05 #3
"Mark" <ma************ *@excite.com> wrote...
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.
Mark

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:FYIKb.7615 79$Tr4.2169809@ attbi_s03...
"Mark" <ma************ *@excite.com> wrote...
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


Jul 22 '05 #4
On Tue, 6 Jan 2004 17:20:03 -0700 in comp.lang.c++, "Mark"
<ma************ *@excite.com> was alleged to have written:
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.

Jul 22 '05 #5
On Wed, 07 Jan 2004 02:49:19 +0000, David Harmon wrote:
On Tue, 6 Jan 2004 17:20:03 -0700 in comp.lang.c++, "Mark"
<ma************ *@excite.com> was alleged to have written:
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.


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

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2187
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should multiple inheritance still be avoided? If yes, why multiple inheritance is introducted into C++?
30
2745
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
22
23392
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
45
6382
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
1
2974
by: vinoraja | last post by:
There are a number of reasons we don't implement Multiple Implementation Inheritance directly. (As you know, we support Multiple Interface Inheritance). However, I should point out that it's possible for compilers to create MI for their types inside the CLR. There are a few rough edges if you go down this path: the result is unverifiable, there is no interop with other languages via the CLS, and in V1 and V1.1 you may run into deadlocks...
47
3671
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
3
2663
by: ernesto | last post by:
Hi everybody I have the following class declarations: class Interface { public: virtual char* getName() const = 0; }; class BaseClass : public Interface {
47
4055
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever support multiple inheritance. In C++ for instance I noticed that the compiler flags an error if you use the "ref" keyword on a class with multiple base classes. This supports the above quote. However, under the "CodeClass2.Bases" property (part...
18
1701
by: GD | last post by:
Please remove ability to multiple inheritance in Python 3000. Multiple inheritance is bad for design, rarely used and contains many problems for usual users. Every program can be designed only with single inheritance. I also published this request at http://bugs.python.org/issue2667
0
9922
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9766
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11065
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10700
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10797
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10391
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7098
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5962
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4177
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.