Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 534663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:38:09+00:00 2026-05-13T09:38:09+00:00

I have some questions about the object size with virtual. 1) virtual function class

  • 0

I have some questions about the object size with virtual.

1) virtual function

class A {
    public:
       int a;
       virtual void v();
    }

The size of class A is 8bytes….one integer(4 bytes) plus one virtual pointer(4 bytes)
It’s clear!

class B: public A{
    public:
       int b;
       virtual void w();
}

What’s the size of class B? I tested using sizeof B, it prints
12

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?

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();
};

The sizeof C is 20……..

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?

My question is, what’s the rule about the number of vptrs in inheritance?

2) virtual inheritance

    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();
};

The sizeof A is 8 bytes ————– 4(int a) + 4 (vptr) = 8

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 ?

The sizeof C is 12 bytes. ————– 4 + 4 + 4 = 12. It’s clear!

The sizeof D is 32 bytes ————– it should be 16(class B) + 12(class C) + 4(int d) = 32. Is that right?

    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();
    };

sizeof A is 8

sizeof B is 16

sizeof C is 16

sizeof D is 28 Does it mean 28 = 16(class B) + 16(class C) – 8(class A) + 4 ( what’s this? )

My question is , why there is an extra space when virtual inheritance is applied?

What’s the underneath rule for the object size in this case?

What’s the difference when virtual is applied on all the base classes and on part of the base classes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:38:10+00:00Added an answer on May 13, 2026 at 9:38 am

    This is all implementation defined. I’m using VC10 Beta2. The key to help understanding this stuff (the implementation of virtual functions), you need to know about a secret switch in the Visual Studio compiler, /d1reportSingleClassLayoutXXX. I’ll get to that in a second.

    The basic rule is the vtable needs to be located at offset 0 for any pointer to an object. This implies multiple vtables for multiple inheritance.

    Couple questions here, I’ll start at the top:

    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?

    This is how virtual functions work, you want the base class and derived class to share the same vtable pointer (pointing to the implementation in the derived class.

    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?

    This is the layout of class C, as reported by /d1reportSingleClassLayoutC:

    class C size(20):
            +---
            | +--- (base class A)
     0      | | {vfptr}
     4      | | a
            | +---
            | +--- (base class B)
     8      | | {vfptr}
    12      | | b
            | +---
    16      | c
            +---
    

    You are correct, there are two vtables, one for each base class. This is how it works in multiple inheritance; if the C* is casted to a B*, the pointer value gets adjusted by 8 bytes. A vtable still needs to be at offset 0 for virtual function calls to work.

    The vtable in the above layout for class A is treated as class C’s vtable (when called through a C*).

    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 ?

    This is the layout of class B in this example:

    class B size(20):
            +---
     0      | {vfptr}
     4      | {vbptr}
     8      | b
            +---
            +--- (virtual base A)
    12      | {vfptr}
    16      | a
            +---
    

    As you can see, there is an extra pointer to handle virtual inheritance. Virtual inheritance is complicated.

    The sizeof D is 32 bytes ————– it should be 16(class B) + 12(class C) + 4(int d) = 32. Is that right?

    No, 36 bytes. Same deal with the virtual inheritance. Layout of D in this example:

    class D size(36):
            +---
            | +--- (base class B)
     0      | | {vfptr}
     4      | | {vbptr}
     8      | | b
            | +---
            | +--- (base class C)
            | | +--- (base class A)
    12      | | | {vfptr}
    16      | | | a
            | | +---
    20      | | c
            | +---
    24      | d
            +---
            +--- (virtual base A)
    28      | {vfptr}
    32      | a
            +---
    

    My question is , why there is an extra space when virtual inheritance is applied?

    Virtual base class pointer, it’s complicated. Base classes are “combined” in virtual inheritance. Instead of having a base class embedded into a class, the class will have a pointer to the base class object in the layout. If you have two base classes using virtual inheritance (the “diamond” class hierarchy), they will both point to the same virtual base class in the object, instead of having a separate copy of that base class.

    What’s the underneath rule for the object size in this case?

    Important point; there are no rules: the compiler can do whatever it needs to do.

    And a final detail; to make all these class layout diagrams I am compiling with:

    cl test.cpp /d1reportSingleClassLayoutXXX
    

    Where XXX is a substring match of the structs/classes you want to see the layout of. Using this you can explore the affects of various inheritance schemes yourself, as well as why/where padding is added, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 381k
  • Answers 381k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer (Edited) Both gcc and MSVC allow 'anonymous' structs/unions, which might… May 14, 2026 at 10:13 pm
  • Editorial Team
    Editorial Team added an answer It looks like Generics might be useful for what you're… May 14, 2026 at 10:13 pm
  • Editorial Team
    Editorial Team added an answer string value = myDictionary.First(v => StringComparer.Create(CultureInfo.CurrentCulture,true) .Compare(v.Key,"AAA") == 0) .Key May 14, 2026 at 10:12 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.