Expressions
4.1 :
unary | binary |
---|---|
& | | + - * |
// box1 + box2
class Box {
public:
int length;
int width;
};
Box operator+(const Box& box1) {
this->length += box1.length;
this->width += box1.width;
return *this;
}
}
overload operator:
- 改变内部操作方式
- 优先级不能改变
- 不能改变操作数数目
左值(location in memory) 和右值(contents)
int &&rval = 20;
4.2 关联性和优先级 precedence & asscotiatity
precedence: + - * /
asscotiatity: (3 + 2) * 5
不要再同一个表达式既改变值又引用它!
Example:
int i = 0;
cout << i << " " << ++i << endl; // unsequenced modification and access to i