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 7929377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:03:18+00:00 2026-06-03T20:03:18+00:00

Can someone please explain about the size of the classes in the case of

  • 0

Can someone please explain about the size of the classes in the case of virtual inheritance involving virtual functions.

   class A{
          char k[ 3 ];
          public:
          virtual void a(){};
          };

   class B : public  A{
          char j[ 3 ];
          public:
          virtual  void b(){};
          };

   class C : public virtual A{
          char i[ 3 ];
          public:
          virtual void c(){};
          };

   class D : public B, public C{
          char h[ 3 ];
          public:
          virtual void d(){};
          };

The output of the size of the classes is :

    sizeof(A): 8
    sizeof(B): 12
    sizeof(C): 16
    sizeof(D): 32

The compiler I am using is
gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)

  • 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-06-03T20:03:19+00:00Added an answer on June 3, 2026 at 8:03 pm

    sizeof(A): 8

    3 bytes in the array, 1 byte padding, 4 bytes for the vptr (pointer to the vtable)

    sizeof(B): 12

    A subobject: 8, 3 bytes for the extra array, 1 byte padding

    sizeof(C): 16

    This is probably the surprising one for you…
    A subobject: 8, 3 bytes for the extra array, 1 byte padding, 4 bytes pointer to A

    Whenever you have virtual inheritance, the location of the virtual base subobject with respect to the start of the complete type is unknown, so an extra pointer is added to the original object to track where the virtual base is. Consider this example:

    struct A {};
    struct B : virtual A {};
    struct C : virtual A {};
    struct D : B, C {};
    

    The location of A with respect to the start of the B object when the complete type is a B can be different than the location of the A subobject of B when it is part of the final object D. If this is not obvious, assume that the relative location is the same, and check whether the relative location of A with respect to C in a C final object or a C subobject in D can also be maintained.

    As for the last example, I don’t quite feel like analyzing it… but you can read the Itanium C++ ABI for a concrete implementation of the C++ object model. All other implementations don’t differ much.


    Last example:

    sizeof(D): 32

    D contains a B subobject (12), and a C subobject (16), plus an additional array of size 3 and one extra bit of padding 1.

    In this particular case, the question that could come up is why are there two A subobjects if C inherits virtually from A, and the answer is that virtual base means that the object is willing to share this base with any other type in the hierarchy that is also willing to share it. But in this case B is not willing to share it’s A subobject, so C needs it’s own.

    You should be able to track this by adding logs to the constructors at the different levels. In the case of A have it take a value in the compiler and pass different values from each extending class.

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

Sidebar

Related Questions

Can someone please explain with example that I can understand about the difference between
Can someone please explain why ?___SID=U is appearing in some Magento URLs on my
Can someone please explain how Read/Show works.. I cannot find any tutorials on it.
Can someone please explain to me how this responsive approach works? This was done
Can someone please explain to me why the hell? When I try to set
Can someone please explain this phenomenon? I am using Mojarra 2.1.6 - Glassfish 3.1.2.
Can someone please explain to me difference between extends and super in java generics
Can someone please explain to me the difference between the AppSettings and ApplicationSettings sections
Can someone please explain how to understand a logcat from an android force close.
Can someone please explain the major differences between Scala, Groovy and Clojure. I know

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.