473,549 Members | 4,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple Inheritance size problem

Hi

When coding classes with multiple inheritance, we faced a problem :

struct a
{
};

struct b
{
};

struct d : public a, public b
{
int m;
}

void foo()
{
int s = sizeof(d); // s == 8 !!!!!
}

We bumped into this problem with Visual .NET 2003, so we tried on
another compiler and platform (namely Metrowerks Codewarrior on MacOSX)
and the code showed just the same... So it must be normal, but why ??

(for the record, the extra dword is not initialized when we look the
raw memory in the debugger (0xcccccccc))

AGoTH
Jul 22 '05 #1
3 1873
Nicolas Hognon wrote:
Hi

When coding classes with multiple inheritance, we faced a problem :

struct a
{
};

struct b
{
};

struct d : public a, public b
{
int m;
}

void foo()
{
int s = sizeof(d); // s == 8 !!!!!
}

We bumped into this problem with Visual .NET 2003, so we tried on
another compiler and platform (namely Metrowerks Codewarrior on MacOSX)
and the code showed just the same... So it must be normal, but why ??

(for the record, the extra dword is not initialized when we look the
raw memory in the debugger (0xcccccccc))

AGoTH


Each of d's subobjects, the a, the b, and m, must have a unique address.
So, an instance of d must span at least enough address space to make
this possible, and to guarantee proper alignment of m. If this makes
you unhappy, Google for the Empty Base Optimization.
Jul 22 '05 #2
Jeff Schwab wrote in news:8Z******** ************@co mcast.com:
Each of d's subobjects, the a, the b, and m, must have a unique
address.
So, an instance of d must span at least enough address space to make
this possible, and to guarantee proper alignment of m. If this makes
you unhappy, Google for the Empty Base Optimization.


This is a matter of debate, Each subobject of the same type *must* have
a unique address.

i.e:

struct empty {};
struct derived : empty
{
empty value;
};

EBO can't work above as that would leave derived::value having the
same address as derived::empty.

VC7.1, and Borland 5.x (with EBO turned on) agrees with you.

g++ 3.2.3 and 3.4 (prerelese) and CBuilder X (preview) (EDG) agrees
with the above.

My test programme:

#include <iostream>
#include <ostream>

struct a
{
};

struct b
{
};

struct d : public a, public b
{
int m;
};
struct empty {};
struct derived : empty
{
empty value;
};

int main()
{
using namespace std;
d dd;
cerr << &dd.m << '\n';
cerr << static_cast< a *>( &dd ) << '\n';
cerr << static_cast< b *>( &dd ) << '\n';

cerr << sizeof(d) << '\n';
derived der;
cerr << &der.value << '\n';
cerr << static_cast< empty *>( &der ) << '\n';

cerr << sizeof(derived) << '\n';
}

Interetingly VC7.1 and Borland 5.x give derived::value the same address
as derived::empty (VC7.1 also gives sizeof( derived ) as 1). All the
others got it write.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
Rob Williscroft wrote in news:Xns9492A50 67AB98ukcoREMOV Efreenetrtw@
195.129.110.130 :
Interetingly VC7.1 and Borland 5.x give derived::value the same address
as derived::empty (VC7.1 also gives sizeof( derived ) as 1). All the
others got it write.


others got it right, (whoops No.2) apparently I need a grammer checker
as well as a compiler before posting.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4

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

Similar topics

20
10043
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in advance for enlightment ... here's the snippet #!/usr/bin/python
3
582
by: Tony Johansson | last post by:
Hello experts! I have a small program that is using multiple inheritance. There are 4 classes involved I get 4 compile error that I can't figure out why. It's this row which is located in the main program see below that is causing these compile errors. cout << p->getName() << endl; Here is the first compile error the other might be a...
22
23324
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?
8
2198
by: Gaetan | last post by:
hi i have 2 classes A1 and A2 implementing a problem with 2 different ways i also have 2 other classes X1 and X2 implementing an other problem i need classes that provide A1+X1 methods, A1+X2, A2+X1 and A2+X2: interface A class A1 implements A
3
2556
by: Hazz | last post by:
I am just beginning to design a Treeview display (winforms) for wine regions. Problem. Some wine growing regions belong to two counties. Eg. Carneros is in both Napa and Sonoma Counties. Although most nodes will have only one parent, a few will have two parents. The Treeview which seems to me to lend itself to the Windows Explorer type of app...
15
28322
by: iKiLL | last post by:
hi all, I would like to be able to create an umbrella class for all my main global sections but I would still like to keep them all in separate file something like the below but I keep getting an error saying you are not allowed Multiple base classes. /// <summary>
7
3722
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance goes like this: Shape | +-- Ellipse | +-- Circle
11
2233
by: John | last post by:
Hi All, Although C# has Generics, it still does not support the generic programming paradigm. Multiple inheritance is required to support real generic programming. Here is a simple design pattern to illustrate this. Problem: I need to expose two lists of objects from a high-level class. I would like to expose these lists as read-only, but...
47
3965
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...
0
198
by: Scott David Daniels | last post by:
Here are some tweaks on both bits of code: Paul McGuire wrote: .... m = for b in bases: if hasattr(b, '__mro__'): if MetaHocObject.ho in b.__mro__: m.append(b) if m:
0
7455
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...
0
7723
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. ...
0
7962
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...
1
7480
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...
0
6050
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...
1
5373
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...
0
5092
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...
0
3504
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...
0
3486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.