gpt4 book ai didi

c++ - #define ^ 在 C++ 中是不允许的

转载 作者:行者123 更新时间:2023-11-30 01:08:26 26 4
gpt4 key购买 nike

我无法使用 #define^ 赋予单独的含义,例如 #define ^ +。但是我可以使用 #define 赋予 $ 意义,比如 #define $ +

您能告诉我$^ 有何不同吗?
根据 c++ 标准的哪条规则,不允许此 ^

我正在使用 VC++ 2012,未尝试使用 GCC 或任何其他工具。

最佳答案

^ 是 C++ 中的运算符。它是按位异或。请阅读https://www.tutorialspoint.com/cplusplus/cpp_operators.htm

运算符不能以这种方式“重新定义”,您需要重载它们。 C++允许在同一范围内为一个函数名或一个运算符指定多个定义,分别称为函数重载和运算符重载(https://www.tutorialspoint.com/cplusplus/cpp_overloading.htmhttps://en.wikibooks.org/wiki/C%2B%2B_Programming/Operators/Operator_Overloading#Bitwise_operators )

对于按位运算符,通常它们的优先级低于算术运算符,因此如果 ^ 被重载以求幂,x ^ y + z 可能无法按预期工作。

XOR 的规范形式是:Type operator^(const Type &lhs, const Type &rhs);//按位异或,而对于成员函数版本:Type &operator^=(const Type &rhs);//分配异或

引用:http://articles.emptycrate.com/2009/10/12/nobody_understands_c_part_8_operator_overloading.html

关于c++ - #define ^ 在 C++ 中是不允许的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42745725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com