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

  • Home
  • SEARCH
  • 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 3485920
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:57:58+00:00 2026-05-18T10:57:58+00:00

#include <iostream> class SuperBase { public: int Sb; }; class Base1:virtual public SuperBase {

  • 0
#include <iostream>
class SuperBase
{
public:
    int Sb;
};
class Base1:virtual public SuperBase
{
public:
    int a;
};
class Base2:virtual public SuperBase
{
public:
    int b;
};
class Derived: public Base1,public Base2
{
public:
    int c;

};
int main()
{
    using namespace std;
    cout<<sizeof(Derived);
    return 0;
}



output is showing 24
but it should show 20 because
int sb 4 bytes
int a 4 bytes
int b 4 bytes
int c 4 bytes
vbptr 4 bytes
total 20 bytes

as we are using virtual inheritance concept so int sb should not be calculated twice isn’t?

  • 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-18T10:57:58+00:00Added an answer on May 18, 2026 at 10:57 am

    24 is probably:

    • 4 for Sb
    • 4 for a
    • 4 for b
    • 4 for c
    • 8 for the overhead of virtual inheritance.

    On my (32bit) compiler, sizeof(Derived) is 24 and sizeof(Base1) is 12, though, suggesting that the overhead isn’t always 8.

    I would guess that the 8 consists of one vtable (or similar) pointer at the start of the Base1 subobject, and another one in the Base2 subobject, which will be used when a pointer to the object is cast to Base2*. The issue with multiple inheritance is that the offset of Base1 from the start of the object can’t be the same as the offset of Base2 from the start of the object.

    In particular, this means that for one of Base1 and Base2, the offset when its a base class subobject of Derived is different from when BaseN is the most derived class. But when you cast to Base2*, the resulting value must point to something that “looks like” a Base2. Hence there’s some extra overhead in cases where virtual inheritance actually results in a shared base class.

    Another way to do it would just be to make sizeof(SuperBase) 4, sizeof(Base1) and sizoef(Base2) both 12 (two ints and a vtable pointer), and sizeof(Derived) equal to 28: 4 for Sb, 4 each for a,b,c, and 4 each for three vtable pointers, one each in Base1, Base2 and Derived. But your compiler has (I think) done you a favour, and arranged for Derived’s vtable pointer to be in the same place as that for Base1, as is normal with single inheritance.

    In all cases when I say “vtable pointer”, it may or may not be the exact same thing that’s used for virtual functions, but it’s some kind of class meta-data. Perhaps it’s just a pointer or offset used to reach a base class.

    I drew some little diagrams here that might help:

    Why can't you use offsetof on non-POD structures in C++?

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

Sidebar

Related Questions

#include <iostream> using namespace std; class A{ int b; public: A(){ cout<<Constructor for class
#include<iostream> using namespace std; class Abc { public: int a; Abc() { cout<<Def cstr
#include <iostream> using namespace std; class CPolygon { protected: int width, height; public: virtual
#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
#include<iostream> using namespace std; class base { public: virtual void add() { cout <<
#include<iostream> using namespace std; class Something { public: int j; Something():j(20) {cout<<Something initialized. j=<<j<<endl;}
#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
#include <iostream> using namespace std; class A { public: A() { cout << Default
#include<iostream> using namespace std; class Base{ private: int a; protected: Base(){}; virtual ~Base(){}; };
#include<iostream> using namespace std; class Abs { public: virtual void hi()=0; }; class B:public

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.