As we mentioned earlier, when there is no user-defined assignment operator, the compiler defaults to generating a shallow copy
If the pointer not only assigns the value to the point, it is also assigned to the address, which causes the problem
Does this question apply to the reference?
#include <iostream>
using namespace std;
Class Mytesttwo {
private:
int value;
int &ref = value; Create a reference to value public
:
mytesttwo (int i) {
value = i;
ref = value;
}
void Myprint () {
cout << ref << Endl;
}
void SetValue (int i) {
value = i;
}
};
int main () {
mytesttwo testa ();
Mytesttwo Testb (a);
Testa.myprint ();
Testb.myprint ();
Testb = Testa; Prompt error
system ("PAUSE");
return 0;
}
This is not allowed, only the user can create the operator
mytesttwo& operator = (const mytesttwo& obj) {
this->value = Obj.value;
return *this;
}
In the following cases, the compiler does not generate the default build operator
(1) Non-static data members in a class that contain const or volatile
(2) Non-static data members that cannot be assigned (such as references)
(3) An inherited base class cannot use an assignment operator (for example, a member of a reference type in a base class)
(4) The class contains pointers