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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:33:08+00:00 2026-06-02T23:33:08+00:00

I wrote this program with virtual inheritance and I have a few questions. #include<iostream>

  • 0

I wrote this program with virtual inheritance and I have a few questions.

#include<iostream>
using namespace std;

class B1
{
 public:
  B1()
  {
    cout << "In B1 constructor\n";
  }
};

class V1 : public B1
{
 public:
  V1()
  {
    cout << "In V1 constructor\n";
  }
};

class D1 : virtual public V1
{
 public:
  D1()
  {
    cout << "In D1 constructor\n";
  }
};

class B2
{
 public:
  B2()
  {
    cout << "In B2 constructor\n";
  }
};

class B3 {
 public:
  B3()
  {
    cout << "In B3 constructor\n";
  }
};

class V2 : public B1, public B2
{
 public:
  V2()
  {
    cout << "In V2 constructor\n";
  }
};

class D2 : public B3, virtual public V2
{
 public:
  D2()
  {
    cout << "In D2 constructor\n";
  }
};

class X : public D1, virtual public D2
{
 public:
  X()
  {
    cout << "In X constructor\n";
  }
};

int main()
{
  X x;
  return 0;
}

Output of the program:

In B1 constructor
In V1 constructor
In B1 constructor
In B2 constructor
In V2 constructor
In B3 constructor
In D2 constructor
In D1 constructor
In X constructor

I expected an output like this:

In B1 constructor
In B2 constructor
In V2 constructor
In B2 constructor
In D2 constructor
In B1 constructor
In V1 constructor
In D1 constructor
In X constructor

on the basis that an object of a virtual base class is constructed first and then the other base class object. Can Someone explain this behaviour?

  • 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-02T23:33:10+00:00Added an answer on June 2, 2026 at 11:33 pm

    The exact quote from the standard is 12.6.2p10:

    In a non-delegating constructor, initialization proceeds in the following order:

    — First, and only for the constructor of the most derived class (1.8), virtual base classes are initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.

    — Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).

    — Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).

    — Finally, the compound-statement of the constructor body is executed.

    I believe the key is the depth-first left to right in the bolded text. The V1 class is a virtual base of X that is to the left of V2, even if it is deeper in the hierarchy.

    The hierarchy graph in your case is like:

                X
              /   \\
            D1     D2
            ||    / \\
            V1  B3   V2
            |       /  \
            B1    B1*   B2
    

    Where single lines identify plain inheritance, and double lines are virtual inheritance. Note that there are two instances of B1 in your complete object X. Now if you perform a depth-first left-to-right search you will walk the nodes in the following order:

    [ B1, V1, D1, B3, B1*, B2, V2, D2, X ]
    

    And the virtual bases are V1, V2, D2, which is the order in which they will be constructed. V1 requires the construction of B1. V2 requires the construction of B1* and B2, D2 requires B3, so the order should be:

    [ B1, V1, B1*, B2, V2, B3, D2, D1, X ]
    

    Where B1 construction is triggered by V1, B1* and B2 must be ordered before V2, B3 is triggered as a dependency of D2. At this point all of the virtual bases are built and the non-virtual bases start to be constructed. The only non-virtual base of X that has not been initialized due to the dependencies of the virtual bases is D1.

    If the diamond was closed (say that V1 and V2 inherited virtually from B1, then there would be only one instance of B1 and it would be the first subobject to be constructed.

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

Sidebar

Related Questions

I wrote this small C++ program and built it(Release) #include<iostream> int main(){ std::cout<<Hello World;
I'm trying to understand concurrency in Go. In particular, I wrote this thread-unsafe program:
I wrote this function to communicate with an external program. Such program takes input
I wrote a program to solve a complicated problem. This program is just a
Below is the program that I wrote. /******************************************************************************* * This program reads EOF from
This is the program I wrote: set serveroutput on; declare b empl.name1%type; r varchar;
I wrote a basic Hippity Hop program in C, Python, and OCaml. Granted, this
I need to write this program using a loop https://i.stack.imgur.com/gko6T.jpg this is what i
I was just doing some stuff and wrote this program. I got the following
I have an app that I wrote using C# .NET 4.0 in Visual Studio

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.