Is it possible to use a bare pointer in a similar way to Unique pointers?

  c++

So I’m currently undertaking a task where a unique pointer would be ideal but I was told to transfer the ownership using a bare pointer and wondering if this is possible? I had a crack at it below but unsure if it’s the correct implementation.

int *p1 = new int(3);
int *p2 = new int;
p2 = p1;

std::cout << p1 << std::endl;
std::cout << p2 << std::endl; //Memory address same as above

delete p1;
p1 = nullptr;

std::cout << p1 << std::endl; //Now returns 0
std::cout << p2 << std::endl; //Same address as above

delete p2;

Source: Windows Questions C++

LEAVE A COMMENT