In the second case , the size of vptr will be added to each and every object . 1st class = one int :4 2nd class = one int +one int from base + vptr =12 3rd class = one int + one int from base + vptr =12 4th class =one int from each base class + one int from each of the grand parent +one vptr =20
On Sun, Jul 3, 2011 at 2:43 PM, himanshu kansal <himanshukansal...@gmail.com > wrote: > class Base > { > public: > int a; > }; > > class X: public Base > { > public: > int x; > }; > > class Y: public Base > { > public: > int y; > }; > > class Z:public X,public Y > { > }; > > int main() > {cout<<sizeof(Base)<<endl; > cout << sizeof(X) <<endl; > cout << sizeof(Y) <<endl; > cout << sizeof(Z) <<endl; > } > o/p is 4 > 8 > 8 > 16... > which is absolutely fine..... > > but the problem arises here.... > class Base > { > public: > int a; > }; > > class X:virtual public Base > { > public: > int x; > }; > > class Y:virtual public Base > { > public: > int y; > }; > > class Z:public X,public Y > { > }; > > int main() > {cout<<sizeof(Base)<<endl; > cout << sizeof(X) <<endl; > cout << sizeof(Y) <<endl; > cout << sizeof(Z) <<endl; > } > > o/p is 4 > 12 > 12 > 20 > why the size is 12 and 20 for x and Z....does this is because that > some sort of VPTR IS maintained in the class....if yes then then what > does that points to.... > > PS:HERE.....i am talking about virtual inheritance....NOT ABOUT > "VIRTUAL FUNCTIONS"... > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.