我想在我的对象中有一个计时器,这可以算作例如。 10 秒后它会 self 毁灭。
我想过这样的事情:
class A{
int interval;
void count(){
/*clock starts putting value in interval*/
if(interval < 10) delete this;
};
};
最佳答案
您应该在程序的主线程中检查删除。
#include <time.h> class A{ time_t m_tCreateTime; int m_iInterval; A(): m_tCreateTime(time()), m_iInterval(10) { } void checkDelete() { /*clock starts putting value in interval*/ if( time() - m_tCreateTime > m_iInterval ) delete this; } };
如果您有一个多线程应用程序并且想要在他自己的线程中独立删除对象,您应该确保您不会在其他线程中使用该对象。
关于c++ - 对象内的定时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42878530/