问题描述:

The Standard defines three kinds of constructors:

— delegating constructor

— target constructor

— principal constructor

12.6.2/6:

The principal constructor is the first constructor invoked in the

construction of an object (that is, not a target constructor for that

object’s construction)

But the same section says:

Once the target constructor returns, the body of the delegating

constructor is executed

Since, a target constructor and a delegating constructor cannot be a principal constructor. So what one is? I would like to consider that by example:

#include <iostream>

using std::cout;

using std::endl;

struct A

{

int a;

A(int a)

{

cout << A::a << endl;

A::a = a;

}

A(int a, int b)

{

cout << A::a << endl;

A::a = a + b;

}

A() : A(10,10)

{

cout << "A()" <<endl;

}

};

A a; //Subsequence of constructor's body execution is A(int, int) --> A()

int main()

{

cout << a.a << endl;

}

demo

What is a principal in the example?

网友答案:

In your example you have

struct A
{
    ...

    A(int a, int b)
    {
        ...
    }

    A() : A(10,10) // A() is a delegating constructor and A(int,int) is the target constructor
    {
        ...
    }
};

A a; 

This means A() is the delegating constructor, A(int,int) is the target constructor.

The standard says (N3690 §12.6.2 - 6)

The principal constructor is the first constructor invoked in the construction of an object (that is, not a target constructor for that object’s construction).

this means A() is both a principal and delegating constructor in your example and that A(int,int), since it's called by a delegating constructor, it's a target constructor and it cannot be a principal constructor.


TL;DR (as suggested by pqnet):

principal  -> the one you invoke
delegating -> the one which calls another constructor
target     -> the one that is called by another constructor

As an unrelated sidenote I agree with Joachim: you're default-initializing a nonstatic member variable and printing its value through scope resolution before its initialization. That is undefined behavior.

网友答案:

Consider the following example

#include <iostream>

int main() 
{
    struct A
    {
        A() : A( 10 ) {}
        A( int x ) : A( x, 20 ) {}
        A( int x, int y ) : x( x ), y( y ) {}
        int x, y;
    };

    A a;

    std::cout << "a.x = " << a.x << std::endl;
    std::cout << "a.y = " << a.y << std::endl;

    return 0;
}

In this example constructor A() is a delegating constructor. It calls constructor A( int ) that in turn also a delegating constructor that calls constructor A( int, int ).

In the definition

A a;

the first constructor that is invoked is A(). It is the principal constructor for this definition because it is called first. That is "The principal constructor is the first constructor invoked in the construction of an object""

If you add one more definition to the body of main as for example

A a2( 5 );

then for this definition the principal constructor is A( int ).

You made a wrong conclusion that a delegating constructor can not be a principal constructor. The first delegating constructor in the chain of delegating constructors is the principal constructor.

相关阅读:
Top