Given this code:
template <class T> Kill (T *& ptr)
{
delete ptr;
ptr = NULL;
};
class MyClass{};
int main()
{
MyClass* myClass = new MyClass;
Kill(myClass);
Kill(myClass);
}
What should happen here?
With my (crappy) Microsoft compiler I executed this and it destructs twice when I have *& but generates an exception when I use * instead of *& in the template definition.
Surely when the pointer is set to NULL calling delete the second time should have no effect. Why was it executed twice with *&
Is it so that the above code should just execute without error and delete the object only once?
template <class T> Kill (T *& ptr)
{
delete ptr;
ptr = NULL;
};
class MyClass{};
int main()
{
MyClass* myClass = new MyClass;
Kill(myClass);
Kill(myClass);
}
What should happen here?
With my (crappy) Microsoft compiler I executed this and it destructs twice when I have *& but generates an exception when I use * instead of *& in the template definition.
Surely when the pointer is set to NULL calling delete the second time should have no effect. Why was it executed twice with *&
Is it so that the above code should just execute without error and delete the object only once?
Comment