是的。要么你不明白如果(cardSession)做了什么,或者你不知道了解删除的作用。对于指针类型,例如你在这里有的变量,条件 if(cardSession) 是检查cardSession是否为空指针 - 它确实 不检查指向对象是否有效(并且没有办法在 一般来说,在标准C ++中执行这样的检查。 随后的删除语句会破坏 cardSession所指向的内容。如果一个有效的物体不存在,你可能会崩溃。 你需要做的是什么,以及什么也是好的做法,就是添加这个删除后: cardSession = null; 还要注意,检查cardSession是否为null不是 必要。删除空指针是绝对安全的(但不是 无效指针),所以只需 删除cardSession; cardSession = null; 代替原来的if我会做的。 -cjm Yes. Either you don''t understand what if(cardSession) does, or you don''tunderstand what delete does. For a pointer type such as the variable youhave here, the conditional if(cardSession) is a check to see whether cardSession is a null pointer or not--it doesNOT check if the pointed-to object is valid (and there is no way ingeneral to perform such a check in standard C++). The subsequent delete statement destroys whatever is pointed to bycardSession. If a valid object isn''t there, you are likely to crash. What you need to do here, and what is also good practice, is to add thisafter you delete: cardSession = null; Also be aware that your check for whether cardSession is null is notnecessary. It is perfectly safe to delete a null pointer (but not aninvalid one), so just delete cardSession;cardSession=null; in place of your original "if" will do the job. -cjm 这篇关于测试以查看对象是否存在新的/删除用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 12:07