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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:23:59+00:00 2026-05-20T08:23:59+00:00

I found this in a website while reading about virtual inheritance in c++ When

  • 0

I found this in a website while reading about virtual inheritance in c++

When multiple inheritance is used, it is sometimes necessary to use virtual inheritance. A good example for this is the standard iostream class hierarchy:

//Note: this is a simplified description of iostream classes

class  ostream: virtual public ios { /*..*/ }
class  istream: virtual public ios { /*..*/ }

class iostream : public istream, public ostream { /*..*/ } 
//a single ios inherited

How does C++ ensure that only a single instance of a virtual member exists, regardless of the number of classes derived from it? C++ uses an additional level of indirection to access a virtual class, usually by means of a pointer. In other words, each object in the iostream hierarchy has a pointer to the shared instance of the ios object. The additional level of indirection has a slight performance overhead, but it’s a small price to pay.

i am confused with the statement:

C++ uses an additional level of indirection to access a virtual class, usually by means of a pointer

could anybody explain this?

  • 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-20T08:24:00+00:00Added an answer on May 20, 2026 at 8:24 am

    Basically, if virtual inheritance is not used, base members are actually part of derived class instances. Memory for the base members is allocated in each instance, and no further indirection is necessary to access them:

    class Base {
    public:
        int base_member;
    };
    
    class Derived: public Base {
    public:
        int derived_member;
    };
    
    
    Derived *d = new Derived();
    int foo = d->derived_member;  // Only one indirection necessary.
    int bar = d->base_member;     // Same here.
    delete d;
    

    However, when virtual inheritance comes into play, virtual base members are shared by all classes in their inheritance tree, instead of several copies being created when the base class is multiply inherited. In your example, iostream only contains one shared copy of the ios members, even though it inherits them twice from both istream and ostream.

    class Base {
    public:
        // Shared by Derived from Intermediate1 and Intermediate2.
        int base_member;  
    };
    
    class Intermediate1 : virtual public Base {
    };
    
    class Intermediate2 : virtual public Base {
    };
    
    class Derived: public Intermediate1, public Intermediate2 {
    public:
        int derived_member;
    };
    

    That means an extra indirection step is required in order to access virtual base members:

    Derived *d = new Derived();
    int foo = d->derived_member;  // Only one indirection necessary.
    int bar = d->base_member;     // Roughly equivalent to
                                  // d->shared_Base->base_member.
    delete d;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this question in an old question in your website so i thought
Reading this question I found this as (note the quotation marks) code to solve
I found this open-source library that I want to use in my Java application.
Just found this out, so i am answering my own question :) Use a
I have found this example on StackOverflow: var people = new List<Person> { new
A while ago, I found in a website some code examples of utility functions
I found this web site (photoblog) http://www.OneReaction.net/ and I am very curious how this
I found this link http://artis.imag.fr/~Xavier.Decoret/resources/glsl-mode/ , but there isn't a lot of description around
I found this in an article on Multithreaded Apartments, but can’t find a definition
I found this guide for using the flash parameters, thought it might be useful

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.