#include<iostream> #include<conio.h> using namespace std; class A { public: int *p; A() { p =new int; } ~A() { delete p; //Is this what i am doing is correct? cout << "in A's destructor"<<endl; } }; int main() { A *obj=new A; delete obj; getch(); } This programs,i have executed in Dev c++ and compiles and executes fine. But i doubt this is not fine.Specially in the destructor where i say delete P
am i wrong?
pwithNULLto avoid undefined behavior ifnew pthrows, and let yourmain()function actually return an int value and you're good to go.new pthrows, then the destructor won't be invoked, and the failed object won't be accessible), and C++ allows you to omit thereturnstatement frommain.