c++ - Writing code for destructor in side derived class in case of multiple inheritance with polymorphism increasing size of Derived class. Why? -


#include <iostream> struct base1 { public:     virtual void show()=0; }; struct base2 { public:     virtual void display()=0; }; class derived:virtual public base1,virtual public base2 { public:     virtual void show(){}     virtual void display(){}  }; void main() {     using namespace std;     cout<<sizeof(derived); } 

output showing 12 when insert destructor of dervied class i.e. following code

#include <iostream> struct base1 { public:     virtual void show()=0; }; struct base2 { public:     virtual void display()=0; }; class derived:virtual public base1,virtual public base2 { public:     virtual void show(){}     virtual void display(){}         ~derived(){}   }; void main() {     using namespace std;     cout<<sizeof(derived); } 

then showing 20 output. why?

1) base classes not have virtual destructor.
2) main return int, not void

what ask implementation defined. using g++ 4.3.0, got same size in both cases (8 bytes should result on 32-bits pc).

edit

under implementation defined, meant depends on how virtual inheritance implemented. usually, derived class contains pointers base classes, that't not necessary case.

in case of g++, able address of every sub-object (pointer base class), size of derived should 12 bytes (on 32-bit machine), because classes empty (i.e. without member variables), compiler free optimize size of empty base classes, , reduce size 8 bytes (not 4 bytes, because needs able provide different address of both base classes).


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -