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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:00:12+00:00 2026-05-29T21:00:12+00:00

Below is a diamond problem faced in multiple inheritance, class Base { public: Base()

  • 0

Below is a diamond problem faced in multiple inheritance,

class Base {
 public:
  Base() {
    cout << "Empty Base constructor " << endl; 
  }
  Base(const string & strVar) { 
    m_strVar = strVar; 
    cout << m_strVar << endl;
  }
  virtual ~Base() {
    cout << "Empty Base destructor " << endl;
  }
  virtual const string & strVar() const { 
   return m_strVar; 
  }

  string m_strVar;
};

class Derived1: public virtual Base {
 public:
  Derived1() {
    cout << "Empty Derived1 constructor " << endl; 
  }
  Derived1(const string & strVar) : Base(strVar) {
    cout << " Derived1 one arg constructor" << endl;
  }
  ~Derived1() {
    cout << "Empty Derived1 destructor " << endl;
  }
};

class Derived2: public virtual Base {
 public:
  Derived2() {
    cout << "Empty Derived2 constructor " << endl; 
  }
  Derived2(const string & strVar) : Base(strVar) {
    cout << "Derived2 one arg constructor" << endl;
  }
  ~Derived2() {
    cout << "Empty Derived2 destructor " << endl;
  }
};

class Derived: public Derived1, public Derived2 {
 public:
  Derived(const string & strVar) : Derived1(strVar), Derived2(strVar) {
    cout << "Derived Constructor " << endl; 
  }
  ~Derived() {
    cout << "Empty Derived destructor " << endl;
  }
};

int main() {
  Derived derObj ("Print this if you can ! "); 
}

the output i get is

  1. Empty Base constructor
  2. Derived2 one arg constructor
  3. Derived1 one arg constructor
  4. Derived Constructor
  5. Empty Derived destructor
  6. Empty Derived2 destructor
  7. Empty Derived1 destructor
  8. Empty Base destructor

i wonder why my derObj parameter i.e “Print this if you can” is not printed and output is not like

  1. Empty Base constructor
  2. Derived2 one arg constructor
  3. Print this if you can!
  4. Derived1 one arg constructor
  5. Derived Constructor
  6. Empty Derived destructor
  7. Empty Derived2 destructor
  8. Empty Derived1 destructor
  9. Empty Base destructor
  • 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-29T21:00:16+00:00Added an answer on May 29, 2026 at 9:00 pm

    This has to do with virtual inheritance.

    When a class is inherited virtually, it is the responsability of the most derived class in the hierarchy to call its constructor: here Derived.

    Since Base is default constructible and you did not precise anything, Derived invokes the default constructor of Base.

    If you want the string to be printed, use:

    Derived(const string & strVar) : Base(strVar), Derived1(strVar), Derived2(strVar)
    {
      std::cout << "Derived Constructor\n"; 
    }
    

    You could remove the default constructor to let the compiler diagnose the issue, though not all compilers give very helpful messages.

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

Sidebar

Related Questions

A typical problem in OO programming is the diamond problem. I have parent class
We know that we can solve the diamond problem using virtual inheritance. For example:
Below is my code. class Program { static void Main(string[] args) { Console.WriteLine(Main thread
Below is my current char* to hex string function. I wrote it as an
Below is my $.ajax call, how do I put a selects (multiple) selected values
Below are lines from the c++ programming language template<class T > T sqrt(T );
Below is my table, a User could have multiple profiles in certain languages, non-English
below is something I am trying to do with JavaScript. If I have string
Below is the db connection class I came out with so far, but I
Below is a Matlab function that implements the triangulation method as described in Multiple

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.