我正在学习C++中的内存管理,但我不明白为什么在离开范围时只调用了一些析构函数.在下面的代码中,当myfunc结束时,只调用obj1析构函数,而不是动态分配的obj2.
int myfunc (cl1 *oarg) {
cout << "myfunc called" << std::endl;
cl1 obj1(222,"NY");
cl1 *obj2;
obj2= new cl1;
oarg->disp();
obj2 -> ~cl1 ;
}
Run Code Online (Sandbox Code Playgroud)
这是我的析构函数:
cl1 :: ~cl1 () {
std::cout<< " cl1 destructor called"<<std::endl;
std::cout << this->data << std::endl; //just to identify the obj
delete [] str;
str = NULL ;
};
Run Code Online (Sandbox Code Playgroud)