问题描述
在对象中实现引用计数时,释放并可能删除对象"原语通常是这样实现的:
When implementing reference counting in objects the "release and possibly delete object" primitive is usually implemented like this:
void CObject::Release()
{
--referenceCount;
if( referenceCount == 0 ) {
delete this;
}
}
首先,删除此
看起来很吓人.但是由于成员函数会立即返回并且不会尝试访问任何成员变量,因此这些东西仍然可以正常工作.至少通常是这样解释的.成员函数甚至可能调用某些全局函数来写入删除了该对象的日志.
First of all, delete this
looks scary. But since the member function returns immediately and doesn't try to access any member variables the stuff still works allright. At least that's how it is explained usually. The member function might even call some global function to write to a log that it deleted the object.
C ++标准是否保证成员函数可以调用删除此
,然后执行不需要访问成员变量和调用成员函数的任何操作,并且这将被定义为正常行为?
Does the C++ standard guarantee that a member function can call delete this
and then do anything that will not require access to member variables and calling member functions and this will be defined normal behaviour?
推荐答案
是的,其行为类似于删除任何其他对象.
Yes, that will behave like deleting any other object.
这篇关于在多大程度上使用“删除此"?符合C ++标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!