The problem is caused by the fact that by not implementing your operator=
you are relying on the default one which will copy the pointer of the temporary object and then destroy it, leaving you with an invalid pointer once the copy is finished.
This is an instance of the set of problems that derive by not following the rule of three (five in C++11). When you can, you should follow the rule of zero, though.
You are trying to reimplement a smart pointer. Just use std::unique_ptr
and it won't compile, like you expect:
struct A { int x,y; A(int x,int y):x(x),y(y){} };
void try_it()
{
std::unique_ptr<A> woop{ new A(8, 11) };
A a{5,3};
woop = a;
}
Live demo