473,748 Members | 6,412 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 2674
"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
2180
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
2723
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
22
23383
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
6356
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
2970
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
3640
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
3
2654
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
4025
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
1688
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
8991
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
8831
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
9548
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...
1
9325
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
9249
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
8244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
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
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.