1
#include<iostream>

using namespace std;

class A
{
    virtual void fun() {}
};

class B
{
    virtual void fun2() {}
};
class C : virtual public  A, virtual public B
{
    public:
        virtual void fun3() {}
};


int main()
{

    /**
     * why is the size of C 16 and not 24?
     */
    cout<<sizeof(A)<<" "<<sizeof(B)<<" "<<sizeof(C);

    return 0;
}

I'm confused as to why the size of C is 16 and not 24. If it inherits the virtual pointers of both class A and B, then why it does not have its own virtual pointer?

Marsroverr
  • 556
  • 1
  • 6
  • 23
  • 5
    Make a class with 1 function, then a class with 10 functions … Is there a size difference? ;) https://godbolt.org/z/_fNBBA – ChrisMM Jun 30 '20 at 13:07
  • Most probably it's optimized away. – Michael Chourdakis Jun 30 '20 at 13:09
  • A class puts all its attributes / functions in private by default I think. Are you sure that class C has access to the functions of its parents? – Sunchock Jun 30 '20 at 13:12
  • @Sunchock whether one can access them or not, they are still there – 463035818_is_not_an_ai Jun 30 '20 at 13:13
  • 2
    Does this answer your question? [C++ virtual function table memory cost](https://stackoverflow.com/questions/1626290/c-virtual-function-table-memory-cost) – user1810087 Jun 30 '20 at 13:15
  • @idclev463035818 You're right but I was wondering if this was not playing on the optimization and suddenly maybe the final size – Sunchock Jun 30 '20 at 13:15
  • 1
    Addition: The question could mean something different, like: If `C` inherits pointers to vtable from `A` and `B` **and** it has a pointer to it's own vtable, why is the size not 3 pointers (24 byte in this case). For this the reason is, the compiler can extend an inherited vtable, thus class `C` still has only 2 pointers to vtables, `A` and `B` but one of them has another virtual function (`fun3`) appended. Source: [how to determine sizeof class with virtual functions?](https://stackoverflow.com/a/4766385/1810087) – user1810087 Jun 30 '20 at 13:52
  • The compiler probably organises vtables in a smarter way than you do. It probably recognises that both `A` and `B` each have a single virtual function, and tucks the "pointer" for `fun3()` into spare space in one or both of the inherited vtables. After all, code that uses `C` (e.g. calls `object->fun3()` where `object` points at an instance of `C` or a class derived from `C`) has visibility that `C` has two bases, each of which has only one virtual function. – Peter Jun 30 '20 at 14:12
  • This is governed by the ABI and not C++ standard. See "Category 3: Virtual Bases Only" in https://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable – Maxim Egorushkin Jun 30 '20 at 14:36
  • FWIW, I don't think virtual inheritance is correct here. Virtual inheritance is usually to help with the [Diamond Problem](https://stackoverflow.com/questions/2659116/how-does-virtual-inheritance-solve-the-diamond-multiple-inheritance-ambiguit) which isn't exactly what you're doing here. I think you want normal inheritance instead. –  Jul 01 '20 at 01:44

0 Answers0