C ++删除语法 [英] C++ delete syntax

查看:120
本文介绍了C ++删除语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个相当不寻常的使用'删除'。

 删除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 than delete, then only the second pointer would be deleted.

这篇关于C ++删除语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆