blldw 2008年09月24日
关于运算符优先级问题
#include <iostream>

class Integer {
public:
Integer(int i = 0) : _i(i) {}

Integer& operator++() {
std::cout << "++res is called" << std::endl;
++_i;
return (*this);
}

operator int() {
return _i;
}

private:
int _i;
};

class A {
public:
bool operator*() {
std::cout << "*A is called" << std::endl;
return true;
}

A& operator++(int) {
std::cout << "A++ is called" << std::endl;
return (*this);
}
};

int main()
{
A a;
Integer i;

if (*a++) ++i;

return 0;
}

执行结果为什么是
A++ is called
*A is called
++res is called
而非
*A is called
A++ is called
++res is called
?
...全文
29 点赞 收藏 5
写回复
5 条回复

还没有回复,快来抢沙发~

发动态
发帖子
C++ 语言
创建于2007-09-28

3.1w+

社区成员

24.8w+

社区内容

C++ 语言相关问题讨论,技术干货分享
社区公告
暂无公告