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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:18:40+00:00 2026-05-27T20:18:40+00:00

//This program is taken from http://www.learncpp.com/cpp-tutorial/114-constructors-and-initialization-of-derived-classes/ #include <iostream> using namespace std; class A {

  • 0
//This program is taken from http://www.learncpp.com/cpp-tutorial/114-constructors-and-initialization-of-derived-classes/
#include <iostream>
using namespace std;

class A
{
public:
    A(int nValue)
    {
        cout << "A: " << nValue << endl;
    }
};

class B: public A
{
public:
    B(int nValue, double dValue)
    : A(nValue)
    {
        cout << "B: " << dValue << endl;
    }
};

int main()
{
    B bClass(5, 4.3);

    return 0;
}

When I run this:

$g++ inherit.cpp -o inherit
$ ./inherit 
A: 5
B: 4.3

which is expected.

But when I change class B to this:

class B: public A
{
int something;
public:
    B(int nValue, double dValue)
    : something(nValue), A(nValue)
    {
        cout << "B: " << dValue << endl;
    cout << "B.something: " << something << endl;
    }
};

the output is:

$ ./inherit 
A: 5
B: 4.3
B.something: 5

My question:

When the compiler goes through B’s initialization list, does it only search for the Base Class constructor and jump to it and not execute the other statements(something(nValue))

or does it execute all the statements till it finds the BaseClass constructor and simply does not execute the constructor body till Base Class constructor is done executing?

In other words would the value of B.something be set to nValue when the something(nValue) statement is first encountered or would it remain uninitialized till A(nValue) is called?

  • 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-27T20:18:40+00:00Added an answer on May 27, 2026 at 8:18 pm

    All sub-objects and non-static member objects of a class instance are initialized in fixed order, namely in the order of their declaration (which is: first all the subobjects in order (e.g. A, B, C for class Foo : A, B, C), then the non-static member objects).

    The order in which you list initializers in the initializer list does not affect this behaviour; however, you should always write the IL in the correct order to avoid surprises and to make your code as easy to read as possible.

    You should generally avoid having one object initializer depend on another object, but sometimes it may be necessary. If possible, just reuse the constructor argument, though. Example:

    struct Foo {
       int a;
       int b;
       Foo(int a_, int b_) : a(a_), b(a) { }   // not so good, depends on order!
       Foo(int a_, int b_) : a(a_), b(a_) { }  // better, always works
    };
    

    Now imagine we had instead struct Foo : Bar, and you might be tempted to write an initializer for Foo as a(a_), b(a), Bar(a, b). That would be a disaster. Writing the IL in order makes this impossible: Bar(a_, b_), a(a_), b(a_).

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

Sidebar

Related Questions

This is the code taken from http://code.google.com/p/crawler4j/ and the name of this file is
This example is taken from the Linux System Programming book, page 88. Code: http://pastebin.com/mEfmHbPP
this is a code snippet taken from https://forums.embarcadero.com/message.jspa?messageID=219481 if FileExists(dstFile) then begin Fs :=
I built a program to loop through words and get their synonyms from www.dicsin.com.br,
I am using an example program from this code http://sicktoolbox.sourceforge.net/ > http://sourceforge.net/projects/sicktoolbox/files/ . It's
I'm using django non-rel ( http://www.allbuttonspressed.com/projects/django-nonrel ) and am trying to delete all the
I found and followed an example from Stackoverflow (http://stackoverflow.com/questions/2310139/how-to-read-xml-response-from-a-url-in-java) of how to read an
OK,First this program can load plist from URL by this code,and I put this
there is this check-in-out program here at my workplace, it only takes the data
I have a program, that takes a long time to load. Because of this,

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.