473,386 Members | 1,830 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.

Doubt regarding Virtual Inheritance

##include <iostream>
using namespace std;

class A
{
};
class B:virtual public A
{
};
class C:virtual public A
{
};
class D:public B, public C
{
};

int main()
{

cout << "Sizeof(A) " << sizeof(A) << endl;
cout << "Sizeof(B) " << sizeof(B) << endl;
cout << "Sizeof(C) " << sizeof(C) << endl;
cout << "Sizeof(D) " << sizeof(D) << endl;
return 0;
}
usually virtual inheritance comes into action to resolve the ambiguty
of data access of grandparent class when there exist more than 1 path
between grandparent and grandchild.

Ok i take it as granted VI resolves data access ambiguity.

But please take look at the output of the program i gave

1
4 //Any virtual pointer to a virtual function table? Note i havent
specified and virtual functions
4 //Any virtual pointer to a virtual function table? Note i havent
specified and virtual functions
8 //Two vptrs of B and C??

if there are vptrs what is their role in avoiding the data access
ambiguity?

i will also give another analysis of memory structure with and without
virtual inheritance.

Jul 6 '07 #1
4 2030
ok this is an upgraded prog of the above one

#include <iostream>
using namespace std;

class A
{
public:
int a;
A():a(10){}
};
class B:public A
{
public:
int b;
B():b(20){}
};
class C:public A
{
public:
int c;
C():c(30){}
};
class D:public B, public C
{
public:
int d;
D():d(40){}
};

int main()
{
int size[4];

size[0] = sizeof(A);
size[1] = sizeof(B);
size[2] = sizeof(C);
size[3] = sizeof(D);

cout << "Sizeof(A) " << size[0] << endl;
cout << "Sizeof(B) " << size[1] << endl;
cout << "Sizeof(C) " << size[2] << endl;
cout << "Sizeof(D) " << size[3] << endl << endl;

/*
* The following operations are done for
* the sake for anatomy of structure of
* memory alignment in an object.
*/

int i = 0;
int count = 0;
int *p = NULL;
cout << "Analysis of object of A" << endl;
A a;
p = (int*)&a;
for(i = 0, count = 0; i < size[0]; i += 4, ++count)
cout << "*(pa + " << count << ") = " << *(p + count) << endl;
cout << endl;

cout << "Analysis of object of B" << endl;
B b;
p = (int*)&b;
for(i = 0, count = 0; i < size[1]; i += 4, ++count)
cout << "*(p + " << count << ") = " << *(p + count) << endl;
cout << endl;

cout << "Analysis of object of C" << endl;
C c;
p = (int*)&c;
for(i = 0, count = 0; i < size[2]; i += 4, ++count)
cout << "*(p + " << count << ") = " << *(p + count) << endl;
cout << endl;

cout << "Analysis of object of D" << endl;
D d;
p = (int*)&d;
for(i = 0, count = 0; i < size[3]; i += 4, ++count)
cout << "*(p + " << count << ") = " << *(p + count) << endl;
cout << endl;

return 0;
}

if you people got time check the program with and without virtual
keyword. You can see virtual pointers are there in action. whats their
role in virtual inheritance even though i didnt declare any virtual
functions?.

I even checked the virtual address table its empty (since no virtual
function). Whats is purpose of a virtual table/pointer in virtual
inheritance even though there is no virtual function?

moderators please pardon for making a huge post...

Jul 6 '07 #2
On 2007-07-06 12:25, Anarki wrote:
ok this is an upgraded prog of the above one

#include <iostream>
using namespace std;

class A
{
public:
int a;
A():a(10){}
};
class B:public A
{
public:
int b;
B():b(20){}
};
class C:public A
{
public:
int c;
C():c(30){}
};
class D:public B, public C
{
public:
int d;
D():d(40){}
};

int main()
{
int size[4];

size[0] = sizeof(A);
size[1] = sizeof(B);
size[2] = sizeof(C);
size[3] = sizeof(D);

cout << "Sizeof(A) " << size[0] << endl;
cout << "Sizeof(B) " << size[1] << endl;
cout << "Sizeof(C) " << size[2] << endl;
cout << "Sizeof(D) " << size[3] << endl << endl;

/*
* The following operations are done for
* the sake for anatomy of structure of
* memory alignment in an object.
*/

int i = 0;
int count = 0;
int *p = NULL;
cout << "Analysis of object of A" << endl;
A a;
p = (int*)&a;
for(i = 0, count = 0; i < size[0]; i += 4, ++count)
cout << "*(pa + " << count << ") = " << *(p + count) << endl;
cout << endl;

cout << "Analysis of object of B" << endl;
B b;
p = (int*)&b;
for(i = 0, count = 0; i < size[1]; i += 4, ++count)
cout << "*(p + " << count << ") = " << *(p + count) << endl;
cout << endl;

cout << "Analysis of object of C" << endl;
C c;
p = (int*)&c;
for(i = 0, count = 0; i < size[2]; i += 4, ++count)
cout << "*(p + " << count << ") = " << *(p + count) << endl;
cout << endl;

cout << "Analysis of object of D" << endl;
D d;
p = (int*)&d;
for(i = 0, count = 0; i < size[3]; i += 4, ++count)
cout << "*(p + " << count << ") = " << *(p + count) << endl;
cout << endl;

return 0;
}

if you people got time check the program with and without virtual
keyword. You can see virtual pointers are there in action. whats their
role in virtual inheritance even though i didnt declare any virtual
functions?.

I even checked the virtual address table its empty (since no virtual
function). Whats is purpose of a virtual table/pointer in virtual
inheritance even though there is no virtual function?
Compiler vendors are free to implement the standard as they choose,
trying to infer things about the language from an implementation is
doomed to fail. The reason you see this behaviour (whatever it is) is
probably because your vendor thought that this implementation was the
best way to implement things.
moderators please pardon for making a huge post...
This is an unmoderated group, and the post is not very large compared to
some others.

--
Erik Wikström
Jul 6 '07 #3
Anarki wrote:
>
Ok i take it as granted VI resolves data access ambiguity.
Virtual inheritance is an implementation technique. Data access
ambiguity is either a design error or a coding error. If it's the result
of a design error, changing to a single base class (using virtual
inheritance) may be the right solution. But it should never be an
automatic reaction to ambiguity.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jul 6 '07 #4
On Jul 6, 6:25 am, Anarki <Deepchan...@gmail.comwrote:
Whats is purpose of a virtual table/pointer in virtual
inheritance even though there is no virtual function?
I'm not a compiler writer, nor am I a guru; however I did read Stanley
Lippman's book: "Inside the C++ Object Module". To paraphrase, he
describes the presence of a virtual table (in this case) as a general
implementation solution to support a form of shared subobject
inheritance when a class has one or more virtual base classes. The
virtual table is used to store the location of invariant and shared
regions of the object. Therefore, its presence (in your case) is the
result of having a virtual base class, not virtual functions.

Jul 6 '07 #5

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

Similar topics

18
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
3
by: Old Monk | last post by:
Hi all, I have got a doubt in Koenig& Moo's AC++. On page 228 and 230, authors provide definitions for Core and Grad classes. Grad IS-A Core, i.e. public inheritance. What puzzles me is that...
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: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
2
by: He Shiming | last post by:
Hi, I've got a question regarding class inheritance. The following code reproduces the problem I'm dealing with: class IBase { public: virtual void Method(void)=0; };
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){;} };
3
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
0
by: =?Utf-8?B?Zmplcm9uaW1v?= | last post by:
Hi all, As I mentioned in a previous thread (see 'Dbghelp, symbols and templates' in microsoft.public.windbg), we created a powerful symbol engine using dbghelp to dump the contents of the stack...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.