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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:37:42+00:00 2026-05-29T04:37:42+00:00

There is this code: #include <iostream> class Base { int x; }; class Derived

  • 0

There is this code:

#include <iostream>

class Base
{
   int x;
};

class Derived : virtual public Base
{
   int y;
};

int main()
{
    std::cout << sizeof(Derived) << std::endl; // prints 12
    return 0;   
}

I have read that when some class is virtually inherited then there is created empty vtable for class Derived, so memory layout is as follows:

Derived::ptr to empty vtable
Derived::y
Base::x

and it is 12 bytes. The question is – what is purpose of this empty vtable if there are not any virtual methods and how is it used?

  • 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-29T04:37:44+00:00Added an answer on May 29, 2026 at 4:37 am

    Derived needs some way to know where the Base subobject is. With virtual inheritance, the relative location of the base class is not fixed with respect to the location of the derived class: it may be located anywhere in the full object.

    Consider a more typical example involving diamond inheritance.

    struct A
    {
        int a;
    };
    
    struct B1 : virtual A
    {
        int b1;
    };
    
    struct B2 : virtual A
    {
        int b2;
    };
    
    struct C : B1, B2
    {
        int c;
    };
    

    Here, both B1 and B2 derive virtually from A, so in C, there is exactly one A subobject. Both B1 and B2 need to know how to find that A subobject (so that they can access the a member variable, or other members of A if we were to define them).

    This is what the vtable is used for in this case: both B1 and B2 will have a vtable that contains the offset of the A subobject.


    To demonstrate what a compiler might do to implement the above diamond inheritance example, consider the following class layouts and virtual tables, generated by the Visual C++ 11 Developer Preview.

    class A size(4):
            +---
     0      | a
            +---
    
    class B1        size(12):
            +---
     0      | {vbptr}
     4      | b1
            +---
            +--- (virtual base A)
     8      | a
            +---
    
    class B2        size(12):
            +---
     0      | {vbptr}
     4      | b2
            +---
            +--- (virtual base A)
     8      | a
            +---
    
    class C size(24):
            +---
            | +--- (base class B1)
     0      | | {vbptr}
     4      | | b1
            | +---
            | +--- (base class B2)
     8      | | {vbptr}
    12      | | b2
            | +---
    16      | c
            +---
            +--- (virtual base A)
    20      | a
            +---
    

    and the following vtables:

    B1::$vbtable@:
     0      | 0
     1      | 8 (B1d(B1+0)A)
    
    B2::$vbtable@:
     0      | 0
     1      | 8 (B2d(B2+0)A)
    
    C::$vbtable@B1@:
     0      | 0
     1      | 20 (Cd(B1+0)A)
    
    C::$vbtable@B2@:
     0      | 0
     1      | 12 (Cd(B2+0)A)
    

    Note that the offsets are relative to the address of the vtable, and note that for the two vtables generated for the B1 and B2 subobjects of C, the offsets are different.

    (Also note that this is entirely an implementation detail–other compilers may implement virtual functions and bases differently. This example demonstrates one way that they are implemented, and they are very commonly implemented this way.)

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

Sidebar

Related Questions

There is this code: #include <iostream> class Base { public: Base(){ std::cout << Constructor
There is this code: #include <iostream> class Bazowa { int x; public: Bazowa() :
I have this code: #include <iostream> using namespace std; class FooA { public: virtual
There is this code: #include <iostream> class KlasaNiePOD{ public: int a; ~KlasaNiePOD(){} }; int
There is this code: #include <iostream> class SomeClass { int someInteger; float someFloat; public:
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
When I compile and run this code: #include <iostream> using namespace std; class Base
There is this code: #include <iostream> class CleverClass{ public: CleverClass() : number(55){} void cleverOperation(){
There is code: #include <iostream> class Int { public: Int() : x(0) {} Int(int
#include<iostream> using namespace std; class A { public: int i; A() {cout<<A()<<endl;} ~A() {cout<<~A()<<endl;}

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.