【发布时间】:2013-07-27 11:04:17
【问题描述】:
我遇到了一些奇怪的问题。我在类方法中使用删除运算符,我想知道如何解决这个问题。
这是代码:
#include <iostream>
using namespace std;
class A
{
public:
int a;
~A() {
cout << "call ~A()" << endl;
}
void action()
{
/* code here */
delete this; //this line depends on some if statements
/* code here */
}
void test2() {
cout << "call test2()" << a << endl;
}
void test() {
a = 10;
cout << "call test()" << endl;
action();
//how can I check if data is deleted?
test2();
}
};
int main()
{
A* a = new A();
a->test();
}
如何检查删除操作符是否删除了数据? 有可能吗?
【问题讨论】:
-
您正在删除类的非静态成员函数中的实例。我认为您需要问自己“我想要实现什么目标?”
-
例如我得到了 gui,当这个窗口的子按钮调用 action closeWindow() 我需要删除窗口,但不知道具体如何,我认为这将是最快的方式或使用某种调度员
-
你需要销毁实际的窗口,但谁说你需要删除代表这个窗口的C++类的实例?如果您正在处理 Windows 消息,这里是some information。我建议您也阅读 cmets,Remy Lebeau 提供了有关如何在 Borland VCL 中完成的有趣见解。
标签: c++ class delete-operator