Originally... Why the parameters of a copy constructor must use a reference type

Source: Internet
Author: User

Originally... Why the parameters of a copy constructor must use a reference type

Transferred from: http://blog.csdn.net/tunsanty/article/details/4264738

In C + +, constructors, copy constructors, destructors and assignment functions (assignment operator overloading) are the most basic knowledge that needs to be mastered. But if I ask you, "Why do I have to use reference types for the parameters of a copy constructor?" "This question, what will you answer?" Perhaps you will answer in order to reduce a memory copy? It is a shame that my first feeling is the same answer. Not good, I have the good quality of reason. After thinking about it, it is wrong to find the answer. Let me rip (a bit of violence, but I like, hey-grin teeth) that hidden in the truth of the small underwear, let it bare in the eyes of the "Children of the Apostles".

Let's start with a small example: (Test yourself to see what the output of this program is?). )

  1. #include <iostream.h>
  2. Class Cexample
  3. {
  4. int m_ntest;
  5. Public
  6. Cexample (int x): m_ntest (x) //with parametric constructor
  7. {
  8. cout << "constructor with argument/n";
  9. }
  10. Cexample (const cexample & Ex) //copy constructor
  11. {
  12. M_ntest = Ex.m_ntest;
  13. cout << "Copy constructor/n";
  14. }
  15. cexample& operator = (const cexample &EX)//Assignment function (Assignment operator overload)
  16. {
  17. cout << "Assignment operator/n";
  18. M_ntest = Ex.m_ntest;
  19. return * this;
  20. }
  21. void Mytestfunc (Cexample ex)
  22. {
  23. }
  24. };
  25. int main ()
  26. {
  27. Cexample aaa (2);
  28. Cexample BBB (3);
  29. BBB = AAA;
  30. Cexample CCC = AAA;
  31. Bbb.mytestfunc (AAA);
  32. return 0;
  33. }

Look at the output of this example:

constructor with argument //Cexample aaa (2);
constructor with argument//Cexample BBB (3);
assignment operator //bbb = AAA;
copy Constructor //Cexample CCC = AAA;
Copy constructor//Bbb.mytestfunc (AAA);

If you can see that this is the result, congratulations, you can stand up and twist your butt, no need to look down.

If your results and output errors, then please be modest to read.

First output: constructor with argument //Cexample aaa (2);

If you do not understand, find someone to drag you out to beat a meal, and then shouted "I am two senior, I am two senior ..."

Second output:constructor with argument //Cexample BBB (3);

Analyze the same as the first one

Third output: assignment operator //bbb = AAA;

Fourth output: copy constructor//cexample CCC = AAA;

These two have to be put together to say. There will certainly be questions about why the two are inconsistent. The reason is that the BBB object has been instantiated, do not need to construct, at this time just to assign AAA to BBB, will only call the assignment function, so simple, still do not understand the words, go to the wall! But CCC has not been instantiated, so the call is a copy of the constructor, the construction of the CCC, rather than the assignment function, still do not understand, I went to the wall!!

Fifth output: copy constructor//Bbb.mytestfunc (AAA);

The AAA is actually passed as a parameter to Bbb.mytestfunc (Cexample ex), that is, cexample ex = AAA, and the fourth consistent, so the copy constructor, not the assignment function, if still do not understand, my head has just bled, do not let me hit again, You just have to put it on your own.

In this example, let's analyze why the parameters of a copy constructor can only use reference types.

See Fourth output: copy constructor//cexample CCC = AAA;

The construction of CCC is essentially the CCC. Cexample (AAA); If the copy constructor parameter is not a reference type, then the CCC will be made. Cexample (AAA) becomes AAA to pass value to CCC. Cexample (Cexample ex), i.e. cexample ex = AAA, since ex is not initialized, cexample ex = AAA continues to call the copy constructor, followed by the construction ex, which is ex. Cexample (AAA), will inevitably have AAA passed to Cexample (Cexample ex), that is, cexample ex = AAA, then trigger the copy constructor, the next forever recursion down.

So the Big Bend is to explain that the parameter of the copy constructor uses the reference type not to reduce the memory copy, but to avoid the duplication of the constructor's infinite recursion.

Originally... Why the parameters of a copy constructor must use 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.