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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:33:45+00:00 2026-06-16T21:33:45+00:00

Possible Duplicate: In C++, what is a virtual base class? In this code when

  • 0

Possible Duplicate:
In C++, what is a virtual base class?

In this code when an object of DR is created, the string “Hello World” should be printed 4 times, instead it is printed just 3 times.Why is it so ?
From what I guess it is because of the mid1 and mid2 being virtually inherited.
Can somebody explain me what happens when we virtually inherit a class and more importantly when it is useful and why ?

#include <iostream>

struct BS
{
  BS()
  {
    std::cout << "Hello World" << std::endl;
  }
  unsigned int color;
};

struct mid1 : virtual public BS { };
struct mid2 : virtual public BS { };
struct mid3 : public BS { };
struct mid4 : public BS { };

struct DR : public mid1, public mid2, 
            public mid3, public mid4 { };

int main(int argc, char** argv) 
{ 
  DR d;
  return 0; 
}
  • 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-16T21:33:46+00:00Added an answer on June 16, 2026 at 9:33 pm

    Let’s look at a simplified example:

    class base {};
    
    class mid1 : public base {};
    class mid2 : public base {};
    
    class derived1 : public mid1, public mid2;
    
    class mid1a : virtual public base {};
    class mid2a : virtual public base {};
    
    class derived2 : public mid1a, public mid2a {};
    

    If we draw object diagrams for these, we get something like this:

    enter image description here

    When the intermediate classes use virtual inheritance, the derived class contains only a single instance of the base class, instead of a separate instance from each intermediate class.

    In your case, that leads to three instances of the base class instead of four.

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

Sidebar

Related Questions

Possible Duplicate: class has virtual functions and accessible non-virtual destructor I got this code
Possible Duplicate: Calling virtual method in base class constructor Calling virtual functions inside constructors
Possible Duplicate: When to use virtual destructors? When should your C++ object's destructor be
Possible Duplicate: Can a member function template be virtual? In a base class, the
Possible Duplicate: Calling virtual functions inside constructors Look at this code. In the constructor
Possible Duplicate: Overload resolution and virtual methods If i call this , why object
Possible Duplicate: Error calling template method in templated-base-class The following code compiles with MSVC10
Possible Duplicate: Virtual Functions and Performance C++ Is this correct, that class member function
Possible Duplicate: Do I need to explicitly call the base virtual destructor? Hello all,
Possible Duplicate: Calling virtual function of derived class from base class constructor? I have

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.