When to call C + + copy constructors and copy constructors (GO)

Source: Internet
Author: User

1. When to invoke the copy constructor

The copy constructor is used to copy an object into the newly created object. That is, it is used during initialization, not during regular assignment. The copy constructor prototype for a class is usually as follows:

Class_name (const class_name&);

It takes a constant reference to a class object as a parameter. For example, the copy constructor of the string class is prototyped as follows:

String (const string&);

When you create a new object and initialize it as an existing object of the same type, the copy constructor is called. This can happen in many cases, and the most common scenario is to initialize the new object to an existing object. For example, assuming that motto is a string object, the following 4 declarations call the copy constructor:

String ditto (motto);

String MeToo = motto;

string also = string (motto);

String *pstring = new string (motto);

Among the 2 declarations in the middle, you might create Metto and also directly using the copy constructor, or you might use a copy constructor to generate a temporary object, and then assign the contents of the temporary object to MeToo and also, depending on the implementation. The last declaration initializes an anonymous object with motto and assigns the address of the new object to the pstring pointer.

2. When to call an assignment constructor

The assignment constructor is implemented by overloading the assignment operator, which is prototyped as follows:

class_name& class_name::operator= (const class_name&);

It accepts and returns a reference to the class object. For example, the assignment operator of the String class is prototyped as follows:

string& string::operator= (const string&);

When you assign an existing object to another object, the overloaded assignment operator is used:

String headline1 ("test");

String knot;

knot = headline1;

The assignment operator is not necessarily used when initializing an object:

String MeToo = knot;

Here, MeToo is a newly created object that is initialized to the value of knot, so the copy constructor is used. However, as noted earlier, it is possible to implement this statement in two steps: Create a temporary object using the copy constructor, and then copy the value of the temporary object to the new object by assigning a value. That is, initialization always invokes the copy constructor, and the assignment operator may be called when using the = operator

The instance code is as follows:

#include <iostream>using namespacestd;classtest{ Public: Test (); ~Test (); Test (Consttest&t); Test&operator=(Consttest&t);Private:    intT1;}; Test::test () {cout<<"Call Constructor"<<Endl;} Test::~Test () {cout<<"Call destructor"<<Endl;} Test::test (Consttest&t) {cout<<"Call the copy constructor"<<Endl;} Test& Test::operator=(Consttest&t) {cout<<"Call Assignment Constructor"<<Endl; T1=t.t1; return* This;}voidMain () {Test T1; Test T2=T1;    Test T3; T3=T1;}

The output is as follows:

Call constructor
Call the copy constructor
Call constructor
Call Assignment constructor
Call destructor
Call destructor
Call destructor

When to call C + + copy constructors and copy constructors (GO)

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.