473,386 Members | 1,708 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,386 software developers and data experts.

Question on virtual inheritance

#include <iostream>

class base {
public: virtual void f() {}
};

class derived: public virtual base {
};

int main()
{
std::cout << "size of base = " << sizeof(base) <<
std::endl; /* =4 . This is Okay.*/
std::cout << "size of derived = " << sizeof(derived) <<
std:endl; /* 4 or 8 */
}

In MS environment (Visual Studio 6/7), the output is coming as 8,
whereas in g++ compiler (g++ 3.4.5), the output is coming as 4.

Can any1 tell how the internals work out for virtual inheritance in
these 2 compilers? (I hv checked that size of pointer =4 in both
systems).
Jun 27 '08 #1
2 1425
am************@gmail.com wrote:
#include <iostream>

class base {
public: virtual void f() {}
};

class derived: public virtual base {
};

int main()
{
std::cout << "size of base = " << sizeof(base) <<
std::endl; /* =4 . This is Okay.*/
std::cout << "size of derived = " << sizeof(derived) <<
std:endl; /* 4 or 8 */
}

In MS environment (Visual Studio 6/7), the output is coming as 8,
whereas in g++ compiler (g++ 3.4.5), the output is coming as 4.

Can any1 tell how the internals work out for virtual inheritance in
these 2 compilers? (I hv checked that size of pointer =4 in both
systems).
Yes, somebody in a G++ newsgroup can tell you about the internals
of the G++ object model and somebody in a VC++ newsgroup can tell
you the internals of the VC++ object model. They are compiler-
specific, implementation detail, yadda-yadda, off-topic really.

A pointer to the virtual base class subojbect[s] is usually stored
in the class (just like a pointer to the virtual function table),
but that's not mandated by the Standard. For all we know, G++ can
see that you only have a single class virtually inheriting from
your base and not store that pointer, and VC++ does store it with
no regard to the completeness of the hierarchy. But that's just
speculation. To know for sure, post to the newsgroup for each
compiler.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
On Apr 24, 3:58 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
ambarish.mi...@gmail.com wrote:
[...]
A pointer to the virtual base class subojbect[s] is usually stored
in the class (just like a pointer to the virtual function table),
There are at least two different solutions: CFront stored a
pointer to the virtual base in the class, but other
implementations put the information necessary to do the fix-up
in the vtable. It's also possible to use thunks to do any
fixing up needed; these would also be in the vtable.

Beyond that, there are a number of variations possible in the
details, and as you said, the only way to find out is to ask in
a forum dedicated to the compiler in question, or to look it up
in the compiler's documentation.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #3

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

Similar topics

4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
3
by: cyrusNew | last post by:
Hi, sorry if it was already asked, I have a following class diagram A =? A | | B C \ /
5
by: Tony Johansson | last post by:
Hello Experts! I just play around just to try to understand this about multiple inheritance. You have all the class definition below and at the bottom you have the main program. So here I...
3
by: marv | last post by:
If I have abstract base classes like these: //--------- class IBase { public: virtual void Action(void) = 0; };
13
by: John Salerno | last post by:
Hi all. I have a question about interfaces now. According to the book I'm reading, when you implement an interface, the class or structure has to declare all the methods that the interface...
14
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
8
by: ^MisterJingo^ | last post by:
Hi all, I have a question regarding inheritance. I'll use the following code for an example (its been stripped down to the minimum): // code start using System; class Animal {
20
by: Daniel | last post by:
I have the following three classes class A { public: virtual void f() = 0; }; class B: public A {
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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,...
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.