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

  • Home
  • SEARCH
  • 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 3623292
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:21:55+00:00 2026-05-18T23:21:55+00:00

The Child constructor in the code below calls its parent constructor to initialise itself.

  • 0

The Child constructor in the code below calls its parent constructor to initialise itself. However the code won’t compile unless Child also calls the Grandparent constructor, even though this is an implementation detail that’s supposed to be hidden within Parent. I don’t want to expose this detail to users of the Child class, as it may change in future. How can I get the code below working?

I tried changing the inheritance to ‘private’, but the Child constructor was still expected to know about this private arrangement, which IMHO somewhat defeats the purpose of private inheritance!

Any suggestions?

#include <iostream>

class MyObject {
  public:
    MyObject(int i) {
      std::cout << "MyObject(" << i << ") constructor" << std::endl;
    }
};

class Grandparent {
  public:
    Grandparent(MyObject i)
    {
    };
};

class Parent: virtual public Grandparent {
  public:
    Parent(int j) :
      Grandparent(MyObject(j))
    {
    };
};

class Child: virtual public Parent {
  public:
    Child() :
      //Grandparent(MyObject(123)),  // Won't work without this
      Parent(5)
    {
    };
};

int main(void)
{
  Child c;
  return 0;
}
$ g++ -o test test.cpp
test.cpp: In constructor ‘Child::Child()’:
test.cpp:29: error: no matching function for call to ‘Grandparent::Grandparent()’
test.cpp:12: note: candidates are: Grandparent::Grandparent(MyObject)
test.cpp:10: note:                 Grandparent::Grandparent(const Grandparent&)
  • 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-18T23:21:56+00:00Added an answer on May 18, 2026 at 11:21 pm

    It’s because the inheritance is virtual. Virtual inheritance can never be an implementation detail because the Child class has to manage the instances of any multiply-inherited class. If you use normal inheritance then this should not be a problem.

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

Sidebar

Related Questions

Why is the Child class's converter constructor called in the code below? I mean,
I'm working with some code that adds a child node to it's parent in
In the below code, can foo 's constructor invoke child1 or child2 's constructor?
Ok, so, with the code below: class Core { public $child; public function start()
In my child window I have $('#opfile').addOption(someval,sometext); Problem is #opfile is an a parent
When you have parent-child tables and you wish to use a DetailsView to edit
I'm creating child processes with fork() in C/C++. When the parent process ends (or
If you have 3 classes, with arrows going from parent to child classes (i.e.
Im trying to debug the following code using System; public class Parent { string
I'm trying to create a child class of TForm with a special constructor for

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.