后缀运算符采用int
参数。已经有一个question as to why ,答案似乎是:“Because Bjarne Stroustrup said so”
我对这个答案感到不舒服。如果 Bjarne Stroustrup 需要一些东西来提示编译器以不同的方式运行,他为什么不能直接关闭运算符是否返回引用?这让我质疑:
- 为什么我不能:
foo++ 13;
- 为什么
int
参数不是默认为 1 - 为什么这被认为是一元运算符,它需要一个参数
最佳答案
If Bjarne Stroustrup needed something to tip the compiler off to behave differently, why couldn't he just key off whether the operator returned a reference?
因为你不能根据函数的返回类型来重载函数。函数可以重载的就是参数、const限定和引用限定
Why can't I do:
foo++ 13;
因为 (int)
参数只是用于重载解析。你不接受它或使用参数。
Why isn't the
int
parameter defaulted to 1
同样,它没有被使用。它只是告诉编译器它是前缀版本还是后缀版本。
Why is this considered a unary operator at all, it takes an argument
它实际上不需要参数。该参数仅用于使它们不同。它只影响和作用于一个操作数,因此它是一元运算符。
关于c++ - 后缀运算符不应该被认为是二元运算符吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37049991/