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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:54:25+00:00 2026-06-02T07:54:25+00:00

I must admit I’m having trouble formulating this question, but I’ll try my best

  • 0

I must admit I’m having trouble formulating this question, but I’ll try my best to be precise. I have tried to search for an answer to my question, but I suspect I have been unable to find what I’m looking for, as I’m not exactly sure what to call this.

I have a base class A, and several child classes inheriting from this base class. I then make another class X that inherits from some of the mentioned child classes. The problem I’m now facing is that each of the classes X inherits, have their own instance of class A. The code below should give a better understanding of what I mean.

class A;
class B : public A;
class C : public A;

class X : public B, public C;

Is there a way to make class B and C share the same instance of class A, when they are both acting as indirect base classes for the same class?

To give an example of why I want this, lets look at this code.

    class A
    {
        int _x;
    };

    class B : public A
    {
        void outputX(){std::cout << A::_x << std::endl;
    };

    class C : public A
    {
       void setX(int x){A::_x=x;}
    };

    class X : public B, public C
    {
        C::setX(5);

        // this will output an un-initialized _x, 
        // as B and C have their own version of A
        B::outputX() 
    };

Now I realize this seems rather unnecessary in this example here, but in my real situation I like to think it would be a good solution if B and C shared instance of A in class X.

Is this at all possible?

  • 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-06-02T07:54:26+00:00Added an answer on June 2, 2026 at 7:54 am

    You can solve this by using virtual inheritance:

    class B : virtual public A;
    class C : virtual public A;
    
    class X : virtual public B, virtual public C;
    

    See more on the diamond problem.

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

Sidebar

Related Questions

This is a bit of an odd case I must admit, but I'm trying
I must admit I have researched a bit but keep coming into a block
I must admit that I don't really know if this is the right place
I have just installed Microsoft Robotics Studio 2008 R2, and I must admit that
I must admit, I am not even sure if I put the question right...
I must admit that I am new to jQuery and JS but I really
I must admit that I'm absolutely new to Expression Trees in C#, but currently
I'm starting to get comfortable with Ruby/Rails but must admit I still look askance
I must admit, this is my first post on this site, so I apologise
I must admit, I don't know much about JavaScript that is why my question

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.