Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ object size with virtual methods
    primarykey
    data
    text
    <p>I have some questions about the object size with virtual.</p> <p><strong>1) virtual function</strong></p> <pre><code>class A { public: int a; virtual void v(); } </code></pre> <p>The size of class A is 8bytes....one integer(4 bytes) plus one virtual pointer(4 bytes) It's clear!</p> <pre><code>class B: public A{ public: int b; virtual void w(); } </code></pre> <p>What's the size of class B? I tested using sizeof B, it prints 12</p> <p>Does it mean that only one vptr is there even both of class B and class A have virtual function? Why there is only one vptr?</p> <pre><code>class A { public: int a; virtual void v(); }; class B { public: int b; virtual void w(); }; class C : public A, public B { public: int c; virtual void x(); }; </code></pre> <p>The sizeof C is 20........</p> <p>It seems that in this case, two vptrs are in the layout.....How does this happen? I think the two vptrs one is for class A and another is for class B....so there is no vptr for the virtual function of class C? </p> <p>My question is, what's the rule about the number of vptrs in inheritance?</p> <p><strong>2) virtual inheritance</strong></p> <pre><code> class A { public: int a; virtual void v(); }; class B: virtual public A{ //virtual inheritance public: int b; virtual void w(); }; class C : public A { //non-virtual inheritance public: int c; virtual void x(); }; class D: public B, public C { public: int d; virtual void y(); }; </code></pre> <p>The sizeof A is 8 bytes -------------- 4(int a) + 4 (vptr) = 8</p> <p>The sizeof B is 16 bytes -------------- Without virtual it should be 4 + 4 + 4 = 12. why there is another 4 bytes here? What's the layout of class B ?</p> <p>The sizeof C is 12 bytes. -------------- 4 + 4 + 4 = 12. It's clear!</p> <p>The sizeof D is 32 bytes -------------- it should be 16(class B) + 12(class C) + 4(int d) = 32. Is that right?</p> <pre><code> class A { public: int a; virtual void v(); }; class B: virtual public A{ //virtual inheritance here public: int b; virtual void w(); }; class C : virtual public A { //virtual inheritance here public: int c; virtual void x(); }; class D: public B, public C { public: int d; virtual void y(); }; </code></pre> <p>sizeof A is 8</p> <p>sizeof B is 16</p> <p>sizeof C is 16</p> <p>sizeof D is 28 Does it mean 28 = 16(class B) + 16(class C) - 8(class A) + 4 ( what's this? )</p> <p>My question is , why there is an extra space when virtual inheritance is applied? </p> <p>What's the underneath rule for the object size in this case? </p> <p>What's the difference when virtual is applied on all the base classes and on part of the base classes?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload