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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:12:17+00:00 2026-05-18T11:12:17+00:00

I was working the last 5 years with the assumption that virtual inheritance breaks

  • 0

I was working the last 5 years with the assumption that virtual inheritance breaks static composition.

But now I discovered, that static composition is still maintained, there is just additional information about the location of the correct instance. Is this right?

  • 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-18T11:12:18+00:00Added an answer on May 18, 2026 at 11:12 am

    Data Layout in non-virtual Inheritance:

    class Point2d {
        int x_, y_;
    };
    
    class Point3d : public Point2d {
        int z_;
    };
    

    Point2d:

    +--------------+
    | int x_       |
    +--------------+
    | int y_       |
    +--------------+
    

    Point3d:

    +--------------+   --+
    | int x_       |     |
    +--------------+     +-- Point2d subobject
    | int y_       |     |
    +--------------+   --+
    | int z_       |
    +--------------+
    

    Point3d is statically composed of Point2d and the member of Point3d.

    Under virtual inheritance

    Implemented with an offset variable inside the object.

    class Point3d : public virtual Point2d {
        int z_;
    };
    

    Point3d:

    +-----------------+
    | int z_          |
    +-----------------+
    | Point2d* _vbase |   --> offset to Point2d subobject (2 in this case)
    +-----------------+   --+
    | int x_          |     |
    +-----------------+     +-- Point2d subobject
    | int y_          |     |
    +-----------------+   --+
    

    Accessing Point3d* point3d->x_ in this context will be translated to (C++ Pseudocode):

    (static_cast<Point2d*>(point3d) + point3d->_vbase)->x_
    

    Note that there are different ways to implement virtual inheritance like offset pointers inside the vtable, this is just one way to implement virtual inheritance. I chose this one because indirection via vtables would require more ascii drawing.

    Virtual inheritance has no benefit here and I would expect (as @Matthieu noted in the comments) a compiler to optimize this class so that it’s internal data layout is the same as in non-virtual inheritance. Virtual inheritance is only beneficial in multiple inheritance (see Vertex3d class below).

    How does this look like in multiple inheritance?

     class Vertex : virtual Point2d {
         Vertex* next_;
     };
    
     class Vertex3d : public Point3d, public Vertex {
     };
    

    Vertex:

    +-----------------+
    | Vertex* next_   |
    +-----------------+
    | Point2d* _vbase |   --> offset of Point2d subobject (2 in this case)
    +-----------------+   --+
    | int x_          |     |
    +-----------------+     +-- Point2d subobject
    | int y_          |     |
    +-----------------+   --+
    

    Vertex3d:

    +------------------+   --+
    | int z_           |     |
    +------------------+     +-- Point3d subobject
    | Point2d* _vbase1 |     |--> offset to Point2d subobject (4 in this case)
    +------------------+   --+
    | Vertex* next_    |     |
    +------------------+     +-- Vertex subobject 
    | Point2d* _vbase2 |     |--> offset to Point2d subobject (2 in this case)
    +------------------+   --+
    | int x_           |     |
    +------------------+     +-- shared Point2d subobject
    | int y_           |     |   both Point3d and Vertex point to this 
    +------------------+   --+   single copy of Point2d
    

    In virtual multiple inheritance both base classes Vertex and Point3d share the base Point2d in Vertex3d. non-virtual inherited members are layed out as usual.

    The point of virtual multiple inheritance is that all descendants of Point3d and Vertex will share one copy of Point2d. Without virtual multiple inheritance (= “ordinary” multiple inheritance) both the Point3d subobject and the Vertex subobject of Vertex3d would have its own copy of Point2d:

    Layout of Vertex3d without virtual multiple inheritance:

    +------------------+   --+
    | int z_           |     |
    +------------------+     +-- Point3d subobject --+
    | int x_           |     |                       |
    +------------------+     |                       +-- Point2d subobject
    | int y_           |     |                       |   of Point3d
    +------------------+   --+                     --+
    | Vertex* next_    |     |
    +------------------+     +-- Vertex subobject  --+
    | int x_           |     |                       |
    +------------------+     |                       +-- Point2d subobject
    | int y_           |     |                       |   of Vertex
    +------------------+   --+                     --+
    

    References:

    • Lippman: Inside the C++ Object Model. Chapter 3
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working with swing for the last 2 years, but i'm still not
I have been working as a native C++ programmer for last few years. Now
I am working on C++ and COM/ATL in Windows from last few years. Now
I have been working on a site from last 2 years. Now my client
For the last 4 years I have been working as an ASP.NET developer in
I've been working with databases for the last few years and I'd like to
I have spent the last several years fighting tooth and nail to avoid working
I have a fantasy football league rails app that was working last year and
I am trying to calculate last years starting day of the current week NOW()
I have a asp.net application which is working from last 2-3 years, it has

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.