C ++ copy constructor

Source: Internet
Author: User

 1. What is a copy constructor?

1. For a class X, if the first parameter of a constructor is one of the following:
A) X &
B) const X &
C) Volatile X &
D) const volatile X &
If no other parameters or other parameters have default values, this function is a copy constructor.

According to this definition, the following are copy constructors of Class X:
X: X (const X &);
X: X (X &, Int = 1 );
Note that these constructors must use references instead of common variables. If common variables are used, a new object needs to be constructed based on the real parameters when passing parameters. To construct this object, you must call the copy constructor, the copy constructor needs to construct an object, so that the infinite loop cannot be constructed. (Refer to a question in the C ++ primer question)
An extra copy constructor is allowed in a class.

Ii. What is the difference between the copy constructor and operator =?

If the object is initialized during definition, the copy constructor is called, regardless of = or.
However, when an object has been constructed and assigned to another object, operator = is called.
Some may ask why the following statements
X;
X = 4;
Will the constructor X (INT) be called, and X = x won't?
It is not difficult to answer this question as long as you understand the assignment process.
We know that X = 4 is actually equivalent to X. Operator = (4), and the compiler will parse the function parameters. Although we do not explicitly overload the = Operator, the compiler will secretly provide operator = (const X &) for us (refer to Article 45 of Objective C ++ ), therefore, the compiler will try to convert 4 from int type to const X & type. Because we provide X (INT), this type conversion can be completed. After the type conversion is completed, the value assignment operation is completed through operator = (const X. That is to say, for X = 4, the compiler actually expands it into the following form:
Const x temp (4 );
X = temp;
We can write this code to test it:
# Include <iostream> <br/> # include <string> <br/> using namespace STD; <br/> Class A {<br/> public: <br/> A () {}< br/> A (int A) {cout <"ctor int" <Endl ;} <br/> A & operator = (const A & A) {cout <"operator = A &" <Endl; return * This ;}< br/> }; <br/> int main () <br/>{< br/> A; <br/> A = 4; <br/> return 0; <br/>}< br/>
The output result is
Ctor int
Opeartor = &
It fully proves the above statement of constructing and then assigning values. Note that if the const is not included in the copy constructor, compilation fails.
So what will happen if operator = (INT) is provided?
If the above Parsing is clear, it is easy to know the result here.
In this case, operator = (INT) is the exact matching function, while calling opeator = (X &) is actually a custom type conversion and then calling operator = (const X &) therefore, the exact matching operator = (INT) will be called first ).
For x = x, because the compiler always provides the default X & operator = (const X &), you cannot use the copy constructor to assign values without initialization.

Reference:
1. http://cpp-circle.group.javaeye.com/group/blog/43289
2. c ++ primer Chinese Third Edition

 

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.