我可以从它的类方法调用析构函数吗? [英] Can i call destructor from its class method?

查看:109
本文介绍了我可以从它的类方法调用析构函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Thread类如下面

 类线程{
public:
Thread ;
〜Thread();
void start();
void stop();
}



因此我需要从stop()方法调用析构函数,

 

code> delete this;

但是要小心。



< / p><

另一种方法是调用析构函数

 〜Thread 

 



,为什么你需要调用析构函数?这是不合逻辑的。您可以编写代码来管理私有方法中的资源并调用它。


I have a Thread class like bellow

class Thread {
public:
  Thread();
  ~Thread();
  void start();
  void stop();
}

So i need to call destructor from stop() method, is it a good way to do that?

解决方案

Yes

delete this;

But be careful. You should not use the deleted object, this and non-static members anymore.

 

Another way is to call destructor

~Thread();

 

But the question is, why do you need call the destructor?! It's not logical. You can write a code to manage the resources in a private method and call it.

这篇关于我可以从它的类方法调用析构函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆