MyClass* A = new MyClass(1);
MyClass* B = A;
/* doing things with A */
// Here I need to reinitialize A (eg. new MyClass(2)) and update B automatically
Is this possible? Or how can this problem be solved?
#include <memory>
#include <iostream>
usingnamespace std;
class MyClass
{
public:
MyClass(int x) : x(x) {}
int x;
};
int main()
{
auto A = make_shared<MyClass>(1);
auto B = A;
cout << B->x << endl; // prints 1
A.reset(new MyClass(2)); // assign new address to A
B = A; // delete old address and re-assign B to A
cout << B->x << endl; // prints 2
return 0;
}