I stumbled across a question that I never thought about before.
Here it is:
each object’s (listed in the initialization list) “constructor” will be triggered.
class B
{
public:
B() { cout<<"B Con\n";}
B(const B &b) { cout<<"B Copy Con\n";}
};
class A
{
public:
A(B &b):_m(b) { cout<<"A Con\n";}
A(const A &a):_m(a._m) { cout<<"A Copy Con\n";}
private:
B _m;
}
main()
{
B b;
A a(b);
}
then I got the output as follows:
B Con
B Copy Con
A Con
According to the output, I think, ‘A a(b)’ triggered B’s copy constructor.
If I got right, then that means ‘A(B &b):_m(b)’ triggers B’s copy constructor.
Why not constructor but copy-constructor?
The reason is when you call
then the copy constructor
is the only one that could match the parameter list. You pass it one parameter and that parameter is of type
class B.Copy constructor is not something super special – it is just a parameterized constructor that will be invoked via the initialization list once the parameter list matches.