A formal parameter in the C + + copy constructor (copy constructor) must be an elaboration of a reference type

Source: Internet
Author: User

In the fourth edition of the C + + primer, the copy constructor (also called the copy constructor) is defined as: a special constructor with a single formal parameter, which is a reference to the class type.

Here's the problem! Why must a formal parameter be a reference to that class type? Instead of a value delivery method? (PS: In fact, the value of the transmission and the address can be unified for the value, the former is the value of the object, the latter is the value of the object's addresses)

Let's look at the following two sets of code:

1.

1 classExample {2 3   Public:4  5 Example () {}6 7Example (Constexample& ex) {}//copy constructor, function body is empty8 9       voidTest (Example param) {}//Test member functionTen     One};

2.

1 classExample {2 3   Public:4  5 Example () {}6 7Example (ConstExample ex) {}//copy constructor, function body is empty8 9       voidTest (Example param) {}//Test member functionTen     One};

It can be seen that the difference between 1 and 2 is the copy constructor of line 7th. In fact, under vs or GCC, the example class in 2 cannot be compiled and will get an error. The reason is that the formal parameter type is a non-reference type.

So what is the problem with the copy constructor defined in the 7th row of 2 in the actual case? is infinite recursion. (PS: The recursion here is somewhat different from the normal recursive return: The normal recursion has a baseline case, that is, recursion can be terminated.) And here the recursive without the benchmark case, it will be infinite recursion down)

To illustrate the problem clearly, let's assume that the code in 2 can be compiled to test the following to see what happens:

3.

1 int Main ()  {  2  3    Example  obj;  // defines an object of the example class obj 4 5    Test (obj);   // call the test member function 6 7    return 0 ; 8 9 Ten }

The 3rd line in 3 defines an object of the example class, and then calls the test function.

That is, test (obj); Because the parameter of test is passed as a value

The prototype of test is:

void Test (Example param);

So, before executing the function body, a copy of the argument to the formal parameter occurs,

i.e. Example param (obj);

But

Example (const Example ex);
Then according to the definition of the Example class in the 2 code, the parameters of the copy constructor are passed by value, so a copy of the actual parameter to the formal parameter is taken, that is, the copy of Example ex (obj);
The copy constructor is then called, for convenience of illustration, this time the copy constructor prototype is: Example (const Example EX1);
it will occur: Example ex1 (obj); obj to ex1 copy
In the same way, this goes on, there will be

Example ex2 (obj),

Example ex3 (obj),

.......,

Example exn (obj),

...........
The endless chamber of posterity is also.

The end result is a stack overflow (stackoverflow) .... Program crashes ....

A formal parameter in the C + + copy constructor (copy constructor) must be an elaboration of a reference type

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.