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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:50:06+00:00 2026-06-06T06:50:06+00:00

In many tutorials describing the usage of virtual base classes (usually used to solve

  • 0

In many tutorials describing the usage of virtual base classes (usually used to solve the diamond problem), they often have a code similar to the design of this structure:

class Animal
{
public:
    Animal()
    {
        cout << "Creating Animal\n";
    }
};

///////////////////////////

class FourLegs : virtual public Animal
{
public:
    FourLegs()
    {
        cout << "Creating FourLegs\n";
    }
};

///////////////////////////

class Mammal : virtual public Animal
{
public:
    Mammal()
    {
        cout << "Creating Mammal\n";
    }
};

///////////////////////////

class Fox : public FourLegs, public Mammal
{
public:
    Fox()
    {
        cout << "Creating Fox\n";
    }
};

When I create an instance of Fox, I get the expected output, only one Animal created:

Creating Animal
Creating FourLegs
Creating Mammal
Creating Fox

As you can see, I have the two tier-2 classes inherit virtually. Now, if only one tier-2 class is inherited virtually, and the other is inherited just publicly, interesting outputs can occur. For example, if if FourLegs is inherited public and Mammal is inherited virtual public, this is the output:

Creating Animal
Creating Animal
Creating FourLegs
Creating Mammal
Creating Fox

This is strange and brings up the question: What is the complete process of creating a class that involves virtual inheritance somewhere in the inheritance tree?

On the other hand, if I FourLegs if inherited virtual public, and Mammal is inherited public, then the output is as just normal (as if nothing was inherited virtual public):

Creating Animal
Creating FourLegs
Creating Animal
Creating Mammal
Creating Fox
  • 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-06T06:50:09+00:00Added an answer on June 6, 2026 at 6:50 am

    Straight from the standard, 12.6.2/10 [class.base.init]:

    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.

    [Note:The declaration order is mandated to ensure that base and member subobjects are destroyed in the reverse order of initialization. —end note]

    The first bullet explains how initialization is done with classes that involve virtual inheritance.

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

Sidebar

Related Questions

I have seen many tutorials, but they're so confusing, and to do what I
I have read many tutorials on the internet about the usage of the 'tr'
I have seen many tutorials on how to compress a single file in c#.
I am relatively new to assembly language. I have found so many tutorials that
I am new to Objective-C programming. In many tutorials I have been seeing code
Okay, so I have searched this site and found many tutorials on how to
I have looked through many tutorials, as well as other question here on stack
I have found many tutorials about using Windows Server 2003 as a development machine,
I've seen many tutorials, but none of them have worked in my case, I
I have seen the word Tombstoning in many tutorials. I did not get what

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.