Copying constructor and calling of default constructor

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.