后自增和预自增运算符的优先级
有代码:
#include <iostream>
class Int {
public:
Int() : x(0) {}
Int(int x_) : x(x_) {}
Int& operator=(const Int& b) {
std::cout << "= from " << x << " = " << b.x << std::endl;
x = b.x;
}
Int& operator+=(const Int& b) {
std::cout << "+= from " << x << " + " << b.x << std::endl;
x += b.x;
return *this;
}
Int& operator++() {
std::cout << "++ prefix " << x << std::endl;
++x;
return *this;
}
Int operator++(int) {
std::cout << "++ postfix " << x << std::endl;
Int result(*this);
++x;
return result;
}
private:
int x;
};
Int operator+(const Int& a, const Int& b) {
std::cout << "operator+" << std::endl;
Int result(a);
result += b;
return result;
}
int main() {
Int a(2), b(3), c(4), d;
d = ++a + b++ + ++c;
return 0;
}
结果:
++ prefix 4
++ postfix 3
++ prefix 2
operator+
+= from 3 + 3
operator+
+= from 6 + 5
= from 0 = 11
为什么后缀运算符不在前缀运算符(++前缀4)之前执行,尽管后缀运算符的优先级高于前缀运算符?
这是由 g++ 编译的。
There is code:
#include <iostream>
class Int {
public:
Int() : x(0) {}
Int(int x_) : x(x_) {}
Int& operator=(const Int& b) {
std::cout << "= from " << x << " = " << b.x << std::endl;
x = b.x;
}
Int& operator+=(const Int& b) {
std::cout << "+= from " << x << " + " << b.x << std::endl;
x += b.x;
return *this;
}
Int& operator++() {
std::cout << "++ prefix " << x << std::endl;
++x;
return *this;
}
Int operator++(int) {
std::cout << "++ postfix " << x << std::endl;
Int result(*this);
++x;
return result;
}
private:
int x;
};
Int operator+(const Int& a, const Int& b) {
std::cout << "operator+" << std::endl;
Int result(a);
result += b;
return result;
}
int main() {
Int a(2), b(3), c(4), d;
d = ++a + b++ + ++c;
return 0;
}
Result:
++ prefix 4
++ postfix 3
++ prefix 2
operator+
+= from 3 + 3
operator+
+= from 6 + 5
= from 0 = 11
Why postfix operator isn't executed before prefix operator (++ prefix 4) altough priority of postfix operator is higher than prefix operator?
This was compiled by g++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
不同操作数的求值顺序未指定,这意味着编译器可以自由地重新排序
++a
、b++
和++c< /code> 子表达式,随意。在此示例中,运算符的优先级实际上并没有任何影响。
如果您尝试编写
++i++
(其中i
是int
),它确实会产生效果,它将被分组为+ +(i++)
并且它将无法编译,因为子表达式i++
是右值,而前缀增量需要左值。如果优先级颠倒,则该表达式将编译(并导致未定义的行为)The order of evaluation of the different operands is unspecified which means that the compiler is free to reorder the evaluation of the
++a
,b++
and++c
subexpressions as it pleases. The precedence of the operators does not really have any impact in that example.It does have an effect if you try to write
++i++
(wherei
is anint
) which will be grouped as++(i++)
and it will fail to compile as the subexpressioni++
is an rvalue and prefix increment requires an lvalue. If the precedence was reversed, then that expression would compile (and cause Undefined Behavior)