Invokation of the chief designer
The standard defines three types of constructors:
โ delegating constructor
โ target constructor
โ principal constructor
12.6.2 / 6:
The main constructor is the first constructor called in the construction of an object (i.e. not the target constructor for that objects)
But the same section says:
After the target constructor returns, the delegate body constructor is executed
Since the assignment constructor and the delegate constructor cannot be the main constructor. So what is it? I would like to consider this with an 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 body execution is A(int, int) --> A()
int main()
{
cout << a.a << endl;
}
What is the principle 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 that A()
- this is a delegating constructor A(int,int)
- is a target constructor.
The standard says (N3690 ยง12.6.2 - 6)
The main constructor is the first constructor called in the construction of an object (i.e. not a target constructor for that object).
this means that it A()
is both the primary and the delegating constructor in your example, and that A(int,int)
since it called the delegating constructor, it is the target constructor and it cannot be the main 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 side-item, I agree with Joachim: you default to initialize a non-statistical member variable and print its value through scope resolution before initializing it. This behavior is undefined.
source to share
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, the constructor A () is the delegating constructor. It calls the constructor A (int), which in turn is also the delegate constructor, which calls the constructor A (int, int).
In the definition
A a;
the first constructor that gets called is A (). This is the main constructor of this definition because it is called the first one. This is "The chief constructor is the first constructor in building an object" "
If you add another definition to the main body like
A a2( 5 );
then A (int) is the main constructor for this definition.
You have made the wrong conclusion that the delegate constructor cannot be the main constructor. The first delegate constructor in the delegating constructor chain is the main constructor.
source to share