C++
#C++ ThisThis behavior is actually specified in the standard (and that's why it was deprecated).
#include<iostream>
#include<memory>
int main()
{
std::auto_ptr<int> a(new int(0));
std::cout<<a.get()<<'\n';
std::auto_ptr<int> b = a;
std::cout<<a.get()<<'\n';
}
Output
some address
0
The process that causes this is the same as Abhijit's answer but without requiring a std::move
and the same as marinus' answer but using a standard class instead of defining it myself.
Edit: I'm adding some explanation. In the output, "some address" will actually be a hex value for the address of the allocated integer. std::auto_ptr
releases its stores pointer when assigned to another auto_ptr
and sets its internal pointer to 0. Calling get()
retrieves access to the stores pointer.