c++ - Object construction and Virtual pointer during mutiple inheritance -


class base1 {      virtual void fun1() { cout << "base1::fun1()" << endl; }      virtual void func1() { cout << "base1::func1()" << endl; }  };  class base2 {      virtual void fun1() { cout << "base2::fun1()" << endl; }      virtual void func1() { cout << "base2::func1()" << endl; }  };   class test:public base1,public base2 { public:     virtual void test(){cout<<"test";} };   typedef void(*fun)(void);   int main() {     test objtest;      fun pfun = null;      pfun = (fun)*((int*)*(int*)((int*)&objtest+0)+0); pfun();      pfun = (fun)*((int*)*(int*)((int*)&objtest+0)+1); pfun();   //the following isnt supposed print test::test() right?     pfun = (fun)*((int*)*(int*)((int*)&objtest+0)+2); pfun();       pfun = (fun)*((int*)*(int*)((int*)&objtest+1)+0); pfun();      pfun = (fun)*((int*)*(int*)((int*)&objtest+1)+1); pfun();   //isnt following supposed print test:test() because order of    construction   object base1 followed construction of base2 followed  construction of test.      pfun = (fun)*((int*)*(int*)((int*)&objtest+1)+2); pfun();  } 

the sizeof test object 8 bytes. evident example object consists of 2 4 byte _vptr's. since order of inheritance public base1,public base2 means object should made in following way:

| _vptr class base1 vtable | -->this base1 vtable should have 2 elements. | _vptr class base2 vtable | -->this base2 vtable should have 3 elements. 

but code snippet looks object made as:

| _vptr class base1 vtable | -->this base1 vtable should have 3 elements. | _vptr class base2 vtable | -->this base2 vtable should have 2 elements. 

the first vptr points array of 3 function pointers(1st points base1::fun1(), 2nd points base1::func1() , third points test::test() ).

a derived object made of base+derived. means first chunk of bytes base object , remaining derived. if so, in our example of objtest , second _vptr should supposed point 3 function pointers (1st base2::fun1(), base2::func1() , test::test() ). see instead first _vptr points function pointer of test::test().

question:

1. behavior compiler specific ?

2. standard mention behavior? or understanding wrong completely?

how vtable constructed compiler dependent. happens when have multiple inheritance.in particular order 2 vtables produced...

there other things stored, beyond vtable - such type information dynamic casting can done.

all required standard "virtual functions work" (sure, it's thousands of words describing "work" means), how it's implemented entirely compiler [and c++ library degree perhaps].


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

json - Gson().fromJson(jsonResult, Myobject.class) return values in 0's -