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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:20:07+00:00 2026-05-29T20:20:07+00:00

Bjarne Stroustrup writes: When defining the functions for a class with a virtual base,

  • 0

Bjarne Stroustrup writes:

When defining the functions for a class with a virtual base, the programmer in general cannot know whether the base will be shared with other derived classes. This can be a problem when implementing a service that requires a base class function to be called exactly once.

I don’t understand this statement.
What possible problems can creep in?

To explain it he gives strange example

class A {                             // no constructor
    // ...
};
class B {
  public:
    B();                              // default constructor
    // ...
};
class C {
  public:
    C(int);                           // no default constructor
};
class D: virtual public A, virtual public B, virtual public C
{
    D() { /*... */ }                  // error: no default constructor for C
    D(int i) : C(i i) { /*... */ };   // ok
    // ...
};   

Is it relevant here??

  • 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-29T20:20:10+00:00Added an answer on May 29, 2026 at 8:20 pm

    Imagine you have a virtual base class that needs to be initialized by calling .initialize(42), exactly once. The point is, you don’t which derived class should call it.

    struct X : virtual A {
      X() { 
         // here 
      }
    };
    
    struct Y : virtual A {
      Y() { 
         // or here 
      }
    };
    
    struct Z : X, Y {
      // what about this    
    };
    

    The correct answer in this case is “sometimes here, sometimes there”, but you don’t know which is the case (depends on which class is the most derived subobject).

    C++ solves this issue for constructors, st. the constructors for all virtual bases are called exactly once in the most derived subobject’s constructor; any constructor in the hierarchy must be prepared to call the virtual base constructor (although at runtime, it might not happen).

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

Sidebar

Related Questions

Bjarne Stroustrup writes in his C++ Style and Technique FAQ , emphasis mine: Because
In Why is there no base class in C++? , I quoted Stroustrup on
I read on the wikipedia page for Null_pointer that Bjarne Stroustrup suggested defining NULL
I'm studying The C++ Programming Language from Bjarne Stroustrup and he talks about logical
In The C++ Programming Language, Bjarne writes that the null pointer is not the
In Bjarne Stroustrup's book The C++ Programming Language, it is stated that a derived
According to Bjarne Stroustrup: if (and only if) you use an initialized member in
I ran into this supposed interview of Bjarne Stroustrup, the inventor of C++. http://artlung.com/smorgasborg/Invention_of_Cplusplus.shtml
According to Bjarne Stroustrup, references were introduced into C++ to support operator overloading: References
Bjarne Stroustrup (C++ creator) once said that he avoids "do/while" loops, and prefers to

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.