473,898 Members | 3,815 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 1894
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
10099
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 consequesce of the first one.
22
23399
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
2218
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
2575
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 could not possibly show one file as a leaf belonging to two different parent folders. You either...
15
28395
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
3748
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
2258
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 require write access internally.
47
4060
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...
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
10859
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
10956
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
10488
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
9662
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
8036
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
7191
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
5882
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...
1
4712
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4298
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.