-2

I have this code:

class A{
  A *prev, *next, *pLast;
  public:
    A(): prev(NULL), next(NULL)
    {
        pLast->next = this;
        pLast = this;
    }
}
class B: public A
{
   int y;
   public:
   B(int y1) : y(y1) {cout << "in cotr" << endl;}
}

I want to know if the contructor of A is called first (even before I insert value for y)? Or first y=y1, then A ctor and in the end B ctor (the printing) thanks!

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
diana
  • 59
  • 1
  • 4
  • Constructors of the parent class(es) are executed first. Actually, the initialization list of B has an implicit call to the (default) constructor of A. `B(int y1) : A(), y(y1) { ....}` and you could use (explicitely) another constructor instead. – Michel Billaud Jul 08 '16 at 06:54
  • 3
    Why not just trying out? – πάντα ῥεῖ Jul 08 '16 at 06:54
  • 1
    Are `cout`s expensive where you live? Why not use a couple in the constructors and see for yourself? – enhzflep Jul 08 '16 at 06:57
  • 1
    @enhzflep Some `cout`s can grow up to 2 meters. – Hatted Rooster Jul 08 '16 at 06:58
  • 1
    Possible duplicate of [Order of calling constructors/destructors in inheritance](http://stackoverflow.com/questions/7539282/order-of-calling-constructors-destructors-in-inheritance) – m8mble Jul 08 '16 at 07:00
  • i tried to add cout but it didnt helped me to understand if y was initialized before the ctor of A called. – diana Jul 08 '16 at 07:21

4 Answers4

1

The base class constructor is called first before members of the derived class are initialized.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

The initialization order is well defined as:

The order of member initializers in the list is irrelevant: the actual order of initialization is as follows:

1) If the constructor is for the most-derived class, virtual base classes are initialized in the order in which they appear in depth-first left-to-right traversal of the base class declarations (left-to-right refers to the appearance in base-specifier lists)

2) Then, direct base classes are initialized in left-to-right order as they appear in this class's base-specifier list

3) Then, non-static data members are initialized in order of declaration in the class definition.

4) Finally, the body of the constructor is executed

So for this case, the initialization order will be (1) the base class A (2) the member y (3) the contructor body of B.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
0

Constructors : base -> most derived

Destructors : most derived -> base

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
0

Here's one way to find out without reading through endless standardese (insert something about fishing and teaching).

You need to print something during the member initialization.
You can do that if your member is also an instance of a class:

struct Member
{
    Member(int i) { cout << "Member initialized" << endl; }
};

struct A
{
    A() { cout << "A initialized" << endl; }
};

struct B : A
{
    B() : member(0) { cout << "B initialized" << endl; }
    Member member;
};

int main()
{
    B b;
}

This prints

A initialized
Member initialized
B initialized

As an aside, this is one situation where it's possible to employ the "comma operator", which lets us avoid that extra class.
Writing B like this:

struct B : A
{
    B() : member((cout << "Member initialized\n", 0)) { cout << "B initialized" << endl; }
    int member;
};

produces the same output.
The comma operator evaluates its left hand side, throws away the result, and returns the right hand side. (It needs an extra pair of parentheses here in order to not be interpreted as two parameters.)
It can be useful when you need to trace evaluation but there's no obvious way to insert tracing.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82