Re: Doubt regarding Virtual Inheritance
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...