In-depth C ++ copy constructor

Source: Internet
Author: User

Let's start with a strange phenomenon I 've encountered recently. Let's look at a piece of code:

# Include <iostream> using namespace STD; Class test {public: Test () {} test (test & T) {DATA = T. data;} test func (test T) {test m; return m;} public: Double Data ;}; int main () {test T1, T2; test T3 = t1.func (T2); Return 0;} the GCC compilation result is as follows:



Does it mean that the parameters provided by the copy constructor do not match those required, however, the form parameters of the copy constructor can only be referenced by the class object but not the object (the reason will be explained later). for call to "test: test (test)" "is hard to understand. Well, we are doing an experiment ,, change the copy constructor to test (test T) {DATA = T. data;} re-compile. The result is as follows:


Ha, it is expected that it cannot be compiled and translated, because the function form parameters of the copy constructor cannot be regarded as their own objects, and they can be referenced at most (as described later), but nothing can be done now, this compilation directly prompts you to change the type you may need to be "test (const Test &)". Follow the prompts to change it to const Test & T test (const Test & T) {DATA = T. data;} the compilation was successful.
To accurately locate the cause of the problem, let's perform another experiment and change the overall code to the following form # include <iostream> using namespace STD; Class test {public: Test () {} test (test & T) {DATA = T. data;} test func (test T) {test m; return m;} public: Double Data ;}; int main () {test T1, T2; /// // test T3; t1.func (T2 ); /// // return 0;} the code is compiled, the copy constructor is still a Test & parameter. However, when calling func in the main function, I didn't use an object to accept the returned value, so it passed, in the past, an object variable was used to accept the function and the return would fail.
Well, now we can locate the error. The most important problem is that the func function returns the result. According to the experiment above, when the function regards the object as the return value and assigns it to another external variable, the copy constructor of the class is called. In this case, the parameter of the copy constructor is "const myclass &" in the form of "myclass &". Why? At that time, I was very confused. Why must I use const ???
------------------------------------------ Gorgeous split ----------------------------------------
Let's take a look at some features of the C ++ copy constructor:

Case of calling the copy constructor

In C ++, the following three objects need to call the copy constructor (sometimes called the copy constructor "):

1)
An object acts as a function parameter and passes in the function body as a value;

2) an object acts as the return value of the function and is returned from the function by passing the value;

3)
An object is used to initialize another object (often called replication initialization );

If you do not use the copy constructor In the first two cases, a pointer is directed to the deleted memory space. In the third case, the difference between initialization and assignment is the reason for calling the copy constructor. In fact, the copy constructor is implemented by the common constructor and the value assignment operator. There are many references describing the similarities and differences between the copy constructor and the value assignment operator.

The general principle is: ① A copy constructor should be provided for classes that contain dynamically allocated members or pointer members; ② while providing a copy constructor, you should also consider overloading the "=" value assignment operator. For the reason, see the following document.

The copy constructor must be passed as a reference (the parameter is the reference value)

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.