Is it possible to use delete operator on class instance without new operator to deallocate memory? Like this:
Class myclass{public void hi{std::cout<<"hi\n";};}
myclass class1;
delete &class1;
class1.hi(); //results in error
Thanks
Is it possible to use delete operator on class instance without new operator to deallocate memory? Like this:
Class myclass{public void hi{std::cout<<"hi\n";};}
myclass class1;
delete &class1;
class1.hi(); //results in error
Thanks
No, the behaviour would be undefined.
You should only use delete
on a pointer that's been given to you by new
. The type of the pointer needs to be the same too, unless it points to an appropriate instance of a polymorphic class.