小编tao*_*tao的帖子

离开范围时不会调用析构函数

我正在学习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)

c++ destructor scope

2
推荐指数
1
解决办法
5749
查看次数

标签 统计

c++ ×1

destructor ×1

scope ×1