小编Nat*_*822的帖子

析构函数的实现作为一个函数?

我有一个关于析构函数的类实现的问题.我理解正确的方法是使用〜运算符,但请看下面的代码:

class foo
{
private:
int* abc;

public:
foo()
{
abc = new int(2);
}

~foo()
{
delete abc;
}

void func()
{
delete abc;
}
}
Run Code Online (Sandbox Code Playgroud)

现在让我们说主要功能定义如下:

int main(int argc, char** argv)
{
foo a;
a.func();
}
Run Code Online (Sandbox Code Playgroud)

在main中调用func()函数时,它的工作方式与析构函数完全相同吗?在任何类似的设置中,析构函数和此函数之间有什么区别?

c++ oop destructor

0
推荐指数
1
解决办法
3531
查看次数

标签 统计

c++ ×1

destructor ×1

oop ×1