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)
- #include <iostream>
- Using namespace std;
- Class Base {
- Public
- Base () {cout << "Constructor invoked!" << Endl;}
- ~base () {cout << "destructor invoked!" << Endl;}
- Base (const base& RHS) {
- cout << "Copy constructor invoked!" << Endl;
- Operator= (RHS); //*this = RHS;
- }
- Base operator= (const base& RHS) { //The problem is here, the return value is not a reference will call the copy constructor
- cout << "Copy assignment operator invoked!" << Endl;
- return * this;
- }
- };
- int main (int argc, char** argv) {
- cout << "Hello World c++!" << Endl;
- Base A;
- Base B (a); //Base B = Base (a);
- return 0;
- }
After modification
- #include <iostream>
- Using namespace std;
- Class Base {
- Public
- Base () {cout << "Constructor invoked!" << Endl;}
- ~base () {cout << "destructor invoked!" << Endl;}
- Base (const base& RHS) {
- cout << "Copy constructor invoked!" << Endl;
- Operator= (RHS); //*this = RHS;
- }
- base& operator= (const base& RHS) { //return reference, can accept
- cout << "Copy assignment operator invoked!" << Endl;
- return * this;
- }
- };
- int main (int argc, char** argv) {
- cout << "Hello World c++!" << Endl;
- Base A;
- Base B (a); //Base B = Base (a);
- return 0;
- }
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)
- #include <iostream>
- Using namespace std;
- Class Base {
- Public
- Base () {cout << "Constructor invoked!" << Endl;}
- ~base () {cout << "destructor invoked!" << Endl;}
- Base (const base& RHS) {
- cout << "Copy constructor invoked!" << Endl;
- }
- base& operator= (const base& RHS) {
- cout << "Copy assignment operator invoked!" << Endl;
- * This= Base (RHS);
- return * this;
- }
- };
- int main (int argc, char** argv) {
- cout << "Hello World c++!" << Endl;
- Base A;
- Base B (a); //Base B = Base (a);
- b = A;
- return 0;
- }
Or that sentence:
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]