473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1864
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********************@comcast.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:Xns9492A5067AB98ukcoREMOVEfreenetrtw@
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
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...
3
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...
22
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...
8
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,...
3
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...
15
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...
7
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...
11
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...
47
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...
0
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.