“删除这个;”这样安全吗? [英] "delete this;" Is this safe?
问题描述
在浏览Flipcode的同时我注意到了这段代码:
class CConsole:public I_TextOutput
{
public:
...
void Release(){删除此; }
...
};
''删除这个;''引起了我的注意。这样的代码安全吗?
完整的文章在这里:
http://www.flipcode.com/articles/art...nplugins.shtml
谢谢,
solosnake
2004年1月10日星期六13:19:55 + 0000,solosnake写道:
在浏览Flipcode时我注意到了这段代码:
class CConsole:public I_TextOutput
{
public:
...
void Release(){删除此; }
...
};
''删除这个;''引起了我的注意。这样的代码安全吗?
是的,确实如此。请确保您事后不要碰任何会员。 Yhis
包括使用数据成员和调用任何非静态函数。
有时这种技术是必要的。我曾经经常使用它。我现在还没有使用过它,我觉得我的设计技巧已经变得更好了。仍有有效用途。
HTH,
M4
" solosnake" < solosnake@solosnake._remove_this_.fsnet.co.uk>写道:
在浏览Flipcode时我注意到这段代码:
class CConsole:public I_TextOutput
{
public:
...
void Release(){删除此; }
...
};
''删除这个;''引起了我的注意。这样的代码是安全的吗?
你能保证在
堆栈上不会分配CConsole对象吗?
solosnake < solosnake@solosnake._remove_this_.fsnet.co.uk>在消息中写道
news:bt ********** @ news5.svr.pol.co.uk ...在浏览Flipcode的同时我注意到了此代码:
class CConsole:public I_TextOutput
{
public:
...
void Release(){删除此; }
...
};
''删除这个;''引起了我的注意。这样的代码安全吗?
完整的文章在这里:
http://www.flipcode.com/articles/art...nplugins.shtml
谢谢,
solosnake
是的,但我称之为糟糕的设计。
原则上,只有创建了
对象应该删除它(或者应该通过将它放在
a句柄类如auto_ptr中来安排删除) - 这允许自定义对象的范围
内存分配而不改变''客户''类。
Whilst browsing Flipcode I noticed this code:
class CConsole: public I_TextOutput
{
public:
...
void Release() { delete this; }
...
};
The ''delete this;'' caught my eye. Is such code safe?
The full article is here :
http://www.flipcode.com/articles/art...nplugins.shtml
Thanks,
solosnake
On Sat, 10 Jan 2004 13:19:55 +0000, solosnake wrote:
Whilst browsing Flipcode I noticed this code:
class CConsole: public I_TextOutput
{
public:
...
void Release() { delete this; }
...
};
The ''delete this;'' caught my eye. Is such code safe?
Yes, it is. Just make sure you don''t touch any member afterwards. Yhis
includes any use of a data member and calling any non static function.
Sometimes this technique is necessary. I used to use it pretty often. I
haven''t used it for some time now, I like to think that my design skills
have gotten better. Still there are valid uses.
HTH,
M4
"solosnake" <solosnake@solosnake._remove_this_.fsnet.co.uk> wrote:
Whilst browsing Flipcode I noticed this code:
class CConsole: public I_TextOutput
{
public:
...
void Release() { delete this; }
...
};
The ''delete this;'' caught my eye. Is such code safe?
Can you guarantee that no CConsole object will ever be allocated on the
stack?
"solosnake" <solosnake@solosnake._remove_this_.fsnet.co.uk> wrote in message
news:bt**********@news5.svr.pol.co.uk...Whilst browsing Flipcode I noticed this code:
class CConsole: public I_TextOutput
{
public:
...
void Release() { delete this; }
...
};
The ''delete this;'' caught my eye. Is such code safe?
The full article is here :
http://www.flipcode.com/articles/art...nplugins.shtml
Thanks,
solosnake
Yes it is but I would call it poor design.
As a matter of principle it is desirable that only the class that created an
object should delete it (or should arrange for its deletion by putting it in
a handle class such as auto_ptr) - this allows scope for custom object
memory allocation without changing ''client'' classes.
这篇关于“删除这个;”这样安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!