473,811 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1445
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...@com Acast.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 objektorientier ter Datenverarbeitu ng
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
2903
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 there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common example: class Vehicle {}
3
1214
by: cyrusNew | last post by:
Hi, sorry if it was already asked, I have a following class diagram A =? A | | B C \ /
5
2115
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 use multiple inheritance. One top class called MBase and below this we have D1 and D2 on the same level.
3
1706
by: marv | last post by:
If I have abstract base classes like these: //--------- class IBase { public: virtual void Action(void) = 0; };
13
3267
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 includes. My question is, if you must declare each method (as opposed to simply 'using' those methods like an #included file), then why not just declare the methods on your own as if you created them yourself? What advantage does using an interface...
14
1932
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
1771
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 the individual elements in an array of ImageList's could be identified by the ID, thereby allowing re-ordering the array without harm. A person could identify by index into the array, but that would not be preserved by re-ordering (and re-ordering...
8
1455
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
2022
by: Daniel | last post by:
I have the following three classes class A { public: virtual void f() = 0; }; class B: public A {
7
2123
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 BufferBase, and customized in order to be able to a type of data (of any kind, such as chunks of audio samples from a sound card). The processor classes are derived from ProcessorBase, and are customized to handle BufferBase-derived objects. Each...
0
9728
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
9605
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
10648
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...
0
10389
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...
0
10135
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
9205
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...
0
6890
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();...
1
4339
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
3867
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.