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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:08:56+00:00 2026-05-13T22:08:56+00:00

(I’m sorry if this has been asked before; the search feature seems to be

  • 0

(I’m sorry if this has been asked before; the search feature seems to be broken: the results area is completely blank, even though it says there are a few pages of results… in Chrome, FireFox, and Safari)

So, I’m just learning C++… and the book I’m moving through is doing a really bad job of explaining constructors in a way that I can grasp them. I’ve pretty much grokked everything else so far, but I can’t figure out how the syntax for constructors actually works.

For instance, I am told that the following will cause the constructor to call the designated superclass’s constructor:

class something : something_else {
  something(int foo, double bar) : something_else(int foo) {}
};

On the other hand, that same syntax was utilized later on in the book, when describing how to initialize const members:

class something : something_else {
private:  const int constant_member;
public:   something(int foo, double bar) : constant_member(42) {}
};

So… uh… what the hell is going on there? What does the syntax rv signature(param) : something_else(what); actually mean? I can’t figure out what that something_else(what) is, with relation to the code around it. It seems to take on multiple meanings; I’m sure there must be some underlying element of the language that it corresponds to, I just can’t figure out what.

Edit: Also, I should mention, it’s very confusing that the what in the previous example is sometimes a parameter list (so something_else(what) looks like a function signature)… and sometimes a constant-value expression (so something_else(what) looks like a function call).

Now, moving on: What about multiple-inheritance and constructors? How can I specify what constructors from which parent classes are called… and which ones are called by default? I’m aware that, by default, the following two are the same… but I’m not sure what the equivalent is when multiple-inheritance is involved:

class something : something_else {
//something(int foo, double bar) : something_else() {}
  something(int foo, double bar) {}
};

Any help in grokking these topics would be very appreciated; I don’t like this feeling that I’m failing to understand something basic. I don’t like it at all.

Edit 2: Okay, the answers below as of now are all really helpful. They raise one more portion of this question though: How do the arguments of base-class-constructor-calls in ‘initialization lists’ relate to the constructor you’re defining? Do they have to match… do there have to be defaults? How much do they have to match? In other words, which of the following are illegal:

class something_else {
  something_else(int foo, double bar = 0.0) {}
  something_else(double gaz) {}
};


class something : something_else {
  something(int foo, double bar)  : something_else(int foo, double bar) {}   };
class something : something_else {
  something(int foo)              : something_else(int foo, double bar) {}   };
class something : something_else {
  something(double bar, int foo)  : something_else(double gaz) {}   };
  • 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-13T22:08:57+00:00Added an answer on May 13, 2026 at 10:08 pm

    The syntax for a constructor definition is:

    Type( parameter-list ) : initialization-list 
    {
       constructor-body
    };
    

    Where the ‘initialization-list’ is a comma separated list of calls to constructors for the bases and/or member attributes. It is required to initialize any subobject (base or member) for which there is no default constructor, constant subobjects and reference attributes, and should be preferred over assignment in the constructor block in all other cases.

    struct base {
       base( int ) {};
    };
    struct base2 {
       base2( int ) {};
    };
    struct type : base, base2
    {
       type( int x ) 
          : member2(x), 
            base2(5), 
            base(1), 
            member1(x*2) 
       { f(); }
       int member1;
       int member2;
    };
    

    The order in which the initialization list is executed is defined in the class declaration: bases in the order in which they are declared, member attributes in the order in which they are declared. In the example above before executing f() in the constructor body the class will initialize its base classes and attributes in the following sequence:

    1. call base(int) constructor with parameter 1
    2. call base2(int) constructor with parameter 5
    3. initialize member1 with value x*2
    4. initialize member2 with value x

    When you throw in virtual inheritance, the virtual base is initialized in the most derived class of the virtual inheritance hierachy, and as such it can (or must if there is no default constructor) appear in that initialization list. In that case, the virtual base will be initialized right before the first subobject that inherits virtually from that base.

    class unrelated {};
    class base {};
    class vd1 : virtual base {};
    class vd2 : virtual base {};
    struct derived : unrelated, vd1, vd2 {
       derived() : unrelated(), base(), vd1(), vd2() {} // in actual order
    };
    

    On Edit 2

    I think you are not reading the details in the answers. The elements in the initialization list are constructor calls, not declarations. The compiler will apply the usual conversion rules for the call if appropriate.

    struct base {
       base( int x, double y );
       explicit base( char x );
    };
    struct derived : base {
       derived() : base( 5, 1.3 ) {}
       derived( int x ) : base( x, x ) {} 
          // will convert x into a double and call base(int,double)
       derived( double d ) : base( 5 ) {} 
          // will convert 5 to char and call base(char)
    // derived( base b ) {} // error, base has no default constructor
    // derived( base b, int x ) : base( "Hi" ) {} 
          // error, no constructor of base takes a const char *
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this question has been asked before. On my search through SO I
Sorry If this has been asked before, but when you search for something on
I'm sorry if this has been asked before, I tried to search and didn't
I'm sorry if this question has been asked before, but I'm not even sure
Sorry if this has been asked before, but I couldn't find a good example
Sorry if this has been asked before, but I'm wondering what the best memory
First of all, sorry if this has been asked before. I've done a pretty
Sorry if this has been asked, my search brought up many off topic posts.
Sorry if this has been asked before, I did check but couldn't find anything...
I'm sorry if this has been asked before, but I'm in kind of a

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.