C + + Copy constructors and overloaded assignment operators invoke each other analysis [go]

Source: Internet
Author: User

Conclusion:
From the perspective of object programming, the copy constructor calls the overloaded assignment operator, and the overloaded assignment operator calls the copy constructor without meaning. should be avoided.

Don ' t try to implement one of the copying functions in terms of the other. Instead, put common functionality in a third function, this both call.

--effective C + + third Edition by Scott Meyers Item 12:copy All parts of a object things to Remember

First, it is clear that the existence meaning of the copy constructor is to construct a new object through the existing object, and then only two objects are constructed; the meaning of the overloaded assignment operator is to assign the value of one object to another object, and two objects are already constructed.

Copy constructor call overload assignment operator: assigns the value of an existing object to an object in a construct, although the object's memory is already allocated. (acceptable, but it is possible to cause cyclic calls to overloaded assignment operators and copy constructors)

The overloaded assignment operator invokes the copy constructor: Copies the existing objects and assigns them to the object. --One more temporary object, and causes the loop to call the overloaded assignment operator.

Example 1: Copy constructor call overload assignment operator (causes loop call overload assignment operator and copy constructor)

  1. #include <iostream>
  2. Using namespace std;
  3. Class Base {
  4. Public
  5. Base () {cout << "Constructor invoked!" << Endl;}
  6. ~base () {cout << "destructor invoked!" << Endl;}
  7. Base (const base& RHS) {
  8. cout << "Copy constructor invoked!" << Endl;
  9. Operator= (RHS);  //*this = RHS;
  10. }
  11. Base operator= (const base& RHS) { //The problem is here, the return value is not a reference will call the copy constructor
  12. cout << "Copy assignment operator invoked!" << Endl;
  13. return * this;
  14. }
  15. };
  16. int main (int argc, char** argv) {
  17. cout << "Hello World c++!" << Endl;
  18. Base A;
  19. Base B (a);  //Base B = Base (a);
  20. return 0;
  21. }

After modification

  1. #include <iostream>
  2. Using namespace std;
  3. Class Base {
  4. Public
  5. Base () {cout << "Constructor invoked!" << Endl;}
  6. ~base () {cout << "destructor invoked!" << Endl;}
  7. Base (const base& RHS) {
  8. cout << "Copy constructor invoked!" << Endl;
  9. Operator= (RHS);  //*this = RHS;
  10. }
  11. base& operator= (const base& RHS) { //return reference, can accept
  12. cout << "Copy assignment operator invoked!" << Endl;
  13. return * this;
  14. }
  15. };
  16. int main (int argc, char** argv) {
  17. cout << "Hello World c++!" << Endl;
  18. Base A;
  19. Base B (a);  //Base B = Base (a);
  20. return 0;
  21. }

This does not have any problem, but it destroys the meaning of the copy constructor (different people may understand the difference), so it is not recommended.

If you think the copy constructor body needs to be assigned this way, Ok, there's no problem with that. Please continue.

Example 2: Overloaded assignment operator calls copy constructor (causes loop call overload assignment operator)

  1. #include <iostream>
  2. Using namespace std;
  3. Class Base {
  4. Public
  5. Base () {cout << "Constructor invoked!" << Endl;}
  6. ~base () {cout << "destructor invoked!" << Endl;}
  7. Base (const base& RHS) {
  8. cout << "Copy constructor invoked!" << Endl;
  9. }
  10. base& operator= (const base& RHS) {
  11. cout << "Copy assignment operator invoked!" << Endl;
  12. * This= Base (RHS);
  13. return * this;
  14. }
  15. };
  16. int main (int argc, char** argv) {
  17. cout << "Hello World c++!" << Endl;
  18. Base A;
  19. Base B (a);  //Base B = Base (a);
  20. b = A;
  21. return 0;
  22. }

Or that sentence:

    • Don ' t try to implement one of the copying functions in terms of the other. Instead, put common functionality in a third function, this both call.

The copy constructor uses an existing object to construct an object that does not exist (after all, the copy constructor is a constructor), which is the initialization of an object. The overloaded function of an assignment operator is to assign a value to another object that already exists and is initialized (that is, the constructor has been initialized).  
For example: String S1 ("Hello"), s2=s1;//copy constructor
sring S1 ("Hello"), S2;
s1=s2;//assignment operator overloading
The copy constructor is called in the following cases:
1. An object is passed into the function body as a value (combined with formal parameters and arguments)
2, an object is returned as a value from the function (the function returns
3, an object needs to be initialized by another object.

C + + Copy constructors and overloaded assignment operators invoke each other analysis [go]

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.