Why does the C + + copy constructor parameter have to be a reference? Must the assignment constructor argument also be a reference?

Source: Internet
Author: User

Prior to writing the copy constructor, the parameter is referenced and not passed for the value, just to reduce the memory copy. Today, however, I see an article that finds that it is wrong to understand the parameters of the copy construction. The argument is a reference and is not passed as a value to prevent infinite recursion of the copy constructor, resulting in a stack overflow.
Let's look at an example:
  
 
  1. class test
  2. {
  3. public:
  4. test()
  5. {
  6. cout << "constructor with argument\n";
  7. }
  8. ~test()
  9. {
  10. }
  11. test(test& t)
  12. {
  13. cout << "copy constructor\n";
  14. }
  15. test&operator=(const test&e)
  16. {
  17. cout << "assignment operator\n";
  18. return *this;
  19. }
  20. };
  21. int _tmain(int argc, _TCHAR* argv[])
  22. {
  23. test ort;
  24. test a(ort);
  25. test b = ort ;
  26. a = b;
  27. return 0;
  28. }
Output: If you can understand this knowledge. Let's explain why value passing is infinitely recursive! If the copy constructor is this:
 
   
  
  1. test(test t);
We call
  
 
  1. test ort;
  2. test a ( ort --> test a ( test t = ort test a ( test t ( ort
  3. -->test.a(test t(test t = ort))
  4. ==test.a(test t(test t(ort)))
  5. -->test.a(test t(test t(test t=ort)))
  6. ...
  7.     就这样会一直无限递归下去。
Here we also understand why the parameters of a copy constructor must be a reference and cannot be passed for a value.
Next, we test the parameters of the assignment constructor, and if we change its parameters to a value pass, do a test.
  
 
  1. class test
  2. {
  3. public:
  4. test()
  5. {
  6. cout << "constructor with argument\n";
  7. }
  8. ~test()
  9. {
  10. }
  11. test(test& t)
  12. {
  13. cout << "copy constructor\n";
  14. }
  15. test&operator=(test e)
  16. {
  17. cout << "assignment operator\n";
  18. return *this;
  19. }
  20. };
  21. int _tmain(int argc, _TCHAR* argv[])
  22. {
  23. test ort;
  24. test a(ort);
  25. test b = ort ;
  26. a = b;
  27. return 0;
  28. }
Output: Assignment constructor If it is passed as a value, it is only one copy more, and does not have infinite recursion.
Summary: The parameters of the copy constructor must be references. An assignment constructor parameter can be either a reference or a value pass, and a value pass will be copied more than once. Therefore, it is recommended that the assignment constructor also be written as a reference type. (Ckk see just now I understand still have deviation: Left and right value is not the key, reduce copy number increase assignment efficiency is the focus)

From for notes (Wiz)

Why does the C + + copy constructor parameter have to be a reference? Must the assignment constructor argument also be a reference?

Related Article

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.