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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:59:28+00:00 2026-06-09T14:59:28+00:00

Chaining constructors across inheritance in C# is handy and useful. What’s the best way

  • 0

Chaining constructors across inheritance in C# is handy and useful. What’s the best way to implement similar functionality in C++? This is what I’d like to express:

struct A
{
    A(int p)
    {
        i = p;
    }

    int i;
};

struct B : virtual A
{
    B(int q) : A(q)
    {
    }
};

struct C : virtual B
{
    C(int r) : B(r) // call B::B() which in turn would call A::A()
    {
    }
};

Any ideas?

Cheers,
Charlie.

  • 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-09T14:59:30+00:00Added an answer on June 9, 2026 at 2:59 pm

    In C++, virtual inheritance requires the constructor of the most derived type to explicitly call the constructor of all the virtual bases:

    C( int r ) : A(r), B(r) {}
    

    While you have clearly stated that you need virtual inheritance, it is uncommon to use virtual inheritance, so you might want to explain the use case. There might be alternatives that don’t require this and will thus allow the chaining of the constructors.


    In case you wondered by the most derived type needs to call the constructor, the reason is that virtual inheritance means there will be only one such base in the complete object. Now if you consider a possible relation:

    struct base { base( int x ); /*...*/ };
    struct d1 : virtual base {
       d1() : base(1) {}
    // ...
    };
    struct d2 : virtual base {
       d2() : base(2) {}
    // ...
    };
    struct final : d1, d2 {};
    

    What value should be passed to constructor of the single instance of base inside final? The two different inheritance paths are passing conflicting values, but there is a single object to initialize. Even if the different calls were not conflicting, with a separate compilation model, the compiler cannot know that, so the only option is that the complete object decides how to initialize the virtual bases.

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

Sidebar

Related Questions

I'm trying to design a system of chaining constructors in a way that I
My motivation for chaining my class constructors here is so that I have a
I'd like to generate method-chaining setters (setters that return the object being set), like
I'm trying to implement method chaining in C++, which turns out to be quite
I'm just getting the concept of chaining constructors down, but I can't figure out
I am chaining together 15 async operations through ports and receivers. This has left
Possible Duplicate: PHP method chaining? I had a function for create user, like cruser
Sorry, but apparently I don't understand chaining enough to figure out this problem... I'm
I would like to smooth the chaining of some jquery.animate functions. Here is a
When chaining constructors, in C#, how can I easily tell whether or not the

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.