Article ID: 122217
Article Last Modified on 7/5/2005
/* Compile options needed: none
*/
#include <stdio.h>
#include <iostream.h>
#include <stddef.h>
class A { public: int a; };
class B : virtual public A { public: int v_a; };
class C : virtual public A { public: int v_b; };
class D : virtual public B, virtual public C { public: int b; };
int main()
{
D xD;
xD.b = 16;
xD.v_a = 8;
xD.v_b = 4;
xD.a = 2;
cout << "sizeof(D) == " << sizeof(D) << endl;
cout << "sizeof(C) == " << sizeof(C) << endl;
cout << "sizeof(B) == " << sizeof(B) << endl;
cout << "sizeof(A) == " << sizeof(A) << endl << endl;
// The following code section works around the offsetof() problem
cout << "offsetof(D.b) == " << (char *)&xD.b - (char *)&xD << endl;
cout << "offsetof(D.v_a) == " << (char *)&xD.v_a - (char *)&xD <<
endl;
cout << "offsetof(D.v_b) == " << (char *)&xD.v_b - (char *)&xD <<
endl;
cout << "offsetof(D.a) == " << (char *)&xD.a - (char *)&xD << endl;
cout << endl;
// The following statement is generated correctly
cout << "offsetof(D.b) == " << offsetof(D, b) << endl;
// The following three statements cause access violations when executed
cout << "offsetof(D.v_a) == " << offsetof(D, v_a) << endl;
cout << "offsetof(D.v_b) == " << offsetof(D, v_b) << endl;
cout << "offsetof(D.a) == " << offsetof(D, a) << endl << endl;
cout << "Press Enter...";
cin.get();
return 0;
}
Additional query words: kbVC400bug 8.00 9.00 buglist1.00 buglist2.00 10.00 10.10 10.20
Keywords: kbprb kbcodegen kbcpponly kbcompiler KB122217