C ++ primer version 4, 619th:
Panda ying_yang ("ying_yang"); // create a panda object
Panda ling_ling = ying_yang // uses copy constructor
At first, I was surprised to call the copy constructor In the second statement. I felt that the value assignment operator function should be called. Otherwise, see the followingCode:
Code
# Include < Iostream >
Using Namespace STD;
Class Base {
Public :
Base (): I ( 0 )
{
Cout < " Default Construction " < Endl;
}
Base ( Const Base & RHS): I (RHS. I)
{
Cout < " Copy Construction " < Endl;
}
Operator = ( Const Base & RHS)
{
I = RHS. I;
Cout < " Copy Operator " < Endl;
}
Private:
IntI;
};
IntMain ()
{
BaseB;
BaseB1=B;
BaseB2;
B2=B;
}
Running result:
Note: panda ling_ling = ying_yang; refers to the replication constructor, because the ling_ling object is not created, initialized, and cannot be assigned a value, therefore, you must execute the copy constructor to create an object.
In addition, once a class defines a constructor, the compiler will not synthesize the default constructor for it.