I do not know what happend when i invoke copy constructor on self. In my opinion, The copy constructor will allocate new memory space for the object, and then copy the data of the old object to the new memory space. So the address will change when i call A(a)
, but it not.
class A {
public:
A(const A& t) {
std::cout << "sss" << endl;
};
A() {};
};
int main() {
A a;
std::cout << &a << endl;
a = A(a);
std::cout << &a << endl;
}
CodePudding user response:
a = A(a);
does create a new A
by invoke copy constructor.
But you then copy(assign) the value back to a
. and drop the temporary.
the a
address is never changed. (there is no way to change it btw)