C ++删除语法 [英] C++ delete syntax
问题描述
我遇到了这个相当不寻常的使用'删除'。
删除ptr1,ptr2
code>
这无疑是一个错误。这里的逗号是逗号运算符,而不是分隔符。只有第一个指针 ptr1
被删除。
第二个指针 ptr2
只是一个无用的表达式。
delete
运算符的优先级高于,
运算符,因此表达式解析为如果它被写成:
(delete ptr1),(ptr2)
pre>
,而不是写成:
delete ptr1,ptr2)
如果
,
优先于delete
,则只会删除第二个指针。I came across this rather unusual usage of 'delete'. Just wanted to know if the following line deletes both pointers or only the first?
delete ptr1, ptr2
解决方案This is undoubtedly an error. The comma here is the comma operator, not a separator. Only the first pointer,
ptr1
is deleted.The second pointer,
ptr2
, is just a do-nothing expression.The
delete
operator has higher precedence than the,
operator, so the expression is parsed as if it were written:(delete ptr1) , (ptr2)
and not as if it were written:
delete (ptr1 , ptr2)
If
,
had higher precedence thandelete
, then only the second pointer would be deleted.这篇关于C ++删除语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!