我正在研究某种智能指针技术,但我缺少一件。我尝试了几种组合,但逻辑如下:
UInt *obj = new UInt;
UInt *ref;
ref = obj;
delete obj;
obj = NULL;
if (ref == NULL)
{
// It works
}
else
{
// It failed
}
有没有办法在不明确将 ref 设置为 NULL 的情况下点击“It Works”?
编辑:
更合适的场景是这样的:
class A
{
public:
A(): ref(NULL) {}
~A()
{
if (ref != NULL)
delete ref;
}
int *ref;
};
int *obj = new int;
A *host = new A();
host->ref = obj; ???
delete obj;
obj = NULL;
if (host->ref == NULL)
{
// It works.
}
else
{
// It failed.
}
...
虽然不能使用 int*& ref 作为类成员.... 必须关闭。