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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:02:03+00:00 2026-05-15T15:02:03+00:00

I was reading the Wikipedia article on virtual inheritance. I followed the whole article

  • 0

I was reading the Wikipedia article on virtual inheritance. I followed the whole article but I could not really follow the last paragraph

This is implemented by providing
Mammal and WingedAnimal with a vtable
pointer (or “vpointer”) since, e.g.,
the memory offset between the
beginning of a Mammal and of its
Animal part is unknown until runtime.
Thus Bat becomes
(vpointer,Mammal,vpointer,WingedAnimal,Bat,Animal).
There are two vtable pointers, one per
inheritance hierarchy that virtually
inherits Animal. In this example, one
for Mammal and one for WingedAnimal.
The object size has therefore
increased by two pointers, but now
there is only one Animal and no
ambiguity. All objects of type Bat
will have the same vpointers, but each
Bat object will contain its own unique
Animal object. If another class
inherits from Mammal, such as
Squirrel, then the vpointer in the
Mammal object in a Squirrel will be
different from the vpointer in the
Mammal object in a Bat, although they
can still be essentially the same in
the special case that the Squirrel
part of the object has the same size
as the Bat part, because then the
distance from the Mammal to the Animal
part is the same. The vtables are not
really the same, but all essential
information in them (the distance) is.

Can someone please shed some more light on this.

  • 1 1 Answer
  • 1 View
  • 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-15T15:02:03+00:00Added an answer on May 15, 2026 at 3:02 pm

    Sometimes, you just really need to see some code / diagrams 🙂 Note that there is no mention of this implementation detail in the Standard.

    First of all, let’s see how to implement methods in C++:

    struct Base
    {
      void foo();
    };
    

    This is similar to:

    struct Base {};
    
    void Base_foo(Base& b);
    

    And in fact, when you look at a method call within a debugger, you’ll often see the this argument as the first parameter. It is sometimes called an implicit parameter.

    Now, on to the virtual table. In C and C++ it is possible to have pointers to function. A vtable is essentially a table of pointers to functions:

    struct Base
    {
      int a;
    };
    
    void Base_set(Base& b, int i) { b.a = i; }
    int Base_get(Base const& b) { return b.a; }
    
    struct BaseVTable
    {
      typedef void (*setter_t)(Base&, int);
      typedef int (*getter_t)(Base const&);
    
      setter_t mSetter;
      getter_t mGetter;
    
      BaseVTable(setter_t s, getter_t g): mSetter(s), mGetter(g) {}
    } gBaseVTable(&Base_set, &Base_get);
    

    Now I can do something like:

    void func()
    {
      Base b;
      (*gBaseVTable.mSetter)(b, 3);
      std::cout << (*gBaseVTable.mGetter)(b) << std::endl; // print 3
    }
    

    Now, on to the inheritance. Let’s create another structure

    struct Derived: Base {}; // yeah, Base does not have a virtual destructor... shh
    
    void Derived_set(Derived& d, int i) { d.a = i+1; }
    
    struct DerivedBaseVTable
    {
      typedef void (*setter_t)(Derived&,int);
      typedef BaseVTable::getter_t getter_t;
    
      setter_t mSetter;
      getter_t mGetter;
    
      DerivedBaseVTable(setter_t s, getter_t g): mSetter(s), mGetter(g) {}
    } gDerivedBaseVTable(&Derived_set, &Base_get);
    

    And the use:

    void func()
    {
      Derived d;
      (*gDerivedBaseVTable.mSetter)(d, 3);
      std::cout << (*gDerivedBaseVTable.mGetter)(d) << std::endl; // print 4
    }
    

    But how to automate this ?

    • you only need one instance of a vtable per class having at least one virtual function
    • each instance of the class will contain a pointer to the vtable as its first attribute (even though you can’t really access it by yourself)

    Now, what happens in case of multi-inheritance ? Well, inheritance is very much like composition in term of memory layout:

    |                                     Derived                                   |
    |                 BaseA                 |                 BaseB                 |
    | vpointer | field1 | field2 | padding? | vpointer | field1 | field2 | padding? |
    

    There will thus be 2 virtual tables for MostDerived: one to change the methods from BaseA and one to change the methods from BaseB.

    Pure virtual functions are generally represented as a null pointer (simply) in the corresponding field.

    And finally, construction and destruction:

    Construction

    • BaseA is constructed: first the vpointer is initialized, then the attributes, then the body of the constructor is executed
    • BaseB is constructed: vpointer, attributes, body
    • Derived is constructed: replace the vpointers (both), attributes, body

    Destruction

    • Derived is destructed: body of the destructor, destroy attributes, put the base vpointers back
    • BaseB is destructed: body, attributes
    • BaseA is destructed: body, attributes

    I think it’s pretty comprehensive, I’d be glad if some C++ gurus around there could review this and check I haven’t made any stupid mistake. Also, if something is missing, I’d be glad to add it.

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

Sidebar

Related Questions

I recently came across this expression - but reading up on Wikipedia did not
According to this Wikipedia article , you are allowed 3,000 files per app but
I have been reading Wikipedia's article on K programming language and this is what
I was reading a wikipedia article on Trimming and saw this implementation of ltrim
So, I was reading about clipping in this Wikipedia article. It seems pretty essential
After reading the wikipedia article on WPF architecture, I am a bit confused with
Reading through the Wikipedia article on First-Class functions, there is a nice table of
After reading the Service Oriented Architecture Principles site and the respective Wikipedia article I
I was recently reading an article on the Windows Metafile vulnerability (http://en.wikipedia.org/wiki/Windows_Metafile_vulnerability#Third-party_patch) and I
I am reading XOR linked list (from Wikipedia).But I am having some problems in

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.