赋值
Assignment (=)
式子从右到左执行。 right-to-left rule).
#include <iostream>
using namespace std;
class B;
class C;
class A{
public:
A(int v){value = v;};
int value;
A& operator=(B& b);
A& operator=(C& c);
A& operator+(B& v);
A& operator+(C& v);
A& operator=(A& v){this->value = v.value; return *this;};
A& operator=(int v){this->value = v; return *this;};
A& operator+(A& v){this->value += v.value; return *this;};
A& operator+(int v){value+=v;return *this;};
};
class B{
public:
B(int v){value = v;};
int value;
B& operator=(A& b);
B& operator=(C& c);
B& operator+(A& b);
B& operator+(C& c);
B& operator=(B& v){this->value = v.value; return *this;};
B& operator=(int v){this->value = v; return *this;};
B& operator+(B& v){this->value += v.value; return *this;};
B& operator+(int v){value+=v;return *this;};
};
class C{
public:
C(int v){value = v;};
int value;
C& operator=(A& b);
C& operator=(B& c);
C& operator+(A& b);
C& operator+(B& c);
C& operator=(C& v){this->value = v.value; return *this;};
C& operator=(int v){this->value = v; return *this;};
C& operator+(C& v){this->value += v.value; return *this;};
C& operator+(int v){value+=v;return *this;};
};
A& A::operator=(B& b)
{
this->value = b.value;
return *this;
}
A& A::operator=(C& c)
{
this->value = c.value;
return *this;
}
A& A::operator+(B& b)
{
this->value += b.value;
return *this;
}
A& A::operator+(C& c)
{
this->value += c.value;
return *this;
}
B& B::operator=(A& v)
{
this->value = v.value;
return *this;
}
B& B::operator=(C& v)
{
this->value = v.value;
return *this;
}
B& B::operator+(A& v)
{
this->value += v.value;
return *this;
}
B& B::operator+(C& v)
{
this->value += v.value;
return *this;
}
C& C::operator=(A& v)
{
this->value = v.value;
return *this;
}
C& C::operator=(B& v)
{
this->value = v.value;
return *this;
}
C& C::operator+(A& v)
{
this->value += v.value;
return *this;
}
C& C::operator+(B& v)
{
this->value += v.value;
return *this;
}
int main()
{
A a(-1); B b(-2); C c(-3);
a = (b=c) + ((b=20) + (c=10));
std::cout << a.value << "\n";
{
//这种情形,如果b 是原生类型,那么,编译器会把b=c, b=20合并在一步b=20。b=c没有效果的。
int a = -1, b = -2, c = -3;a = (b=c) + ((b=20) + (c=10));
std::cout << a << "\n";
}
return 0;
};
算术运算符
Arithmetic operators
+ | addition |
- | subtraction |
* | multiplication |
/ | division |
% | modulo |
复合运算符
Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)
自增减
Increase and decrease (++, --)
相对相等性
Relational and equality operators ( ==, !=, >, <, >=, <= )
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
逻辑操作符
注意:算数运算符是有到左,但是逻辑操作是左到右,发现条件成立即刻停止。
Logical operators ( !, &&, || )
&& OPERATOR
a | b | a && b |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
|| OPERATOR
a | b | a || b |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
!true // evaluates to false
!false // evaluates to true.
条件三位操作符
Conditional operator ( ? )
a ? b : c;
逗号操作符
Comma operator ( , )
a = (b=3, b+2);
位操作符
Bitwise Operators ( &, |, ^, ~, <<, >> )
operator | asm equivalent | description |
---|---|---|
& | AND | Bitwise AND |
| | OR | Bitwise Inclusive OR |
^ | XOR | Bitwise Exclusive OR |
~ | NOT | Unary complement (bit inversion) |
<< | SHL | Shift Left |
>> | SHR | Shift Right |
显示类型转换
int i;
float f = 3.14;
i = (int) f;
i = int(f);
长度
sizeof()
其他操作符
Other operators
运算子优先级
Precedence of operators
Level | Operator | Description | Grouping |
---|---|---|---|
1 | :: | scope | Left-to-right |
2 | () [] . -> ++ -- dynamic_cast static_cast reinterpret_cast const_cast typeid | postfix | Left-to-right |
3 | ++ -- ~ ! sizeof new delete | unary (prefix) | Right-to-left |
* & | indirection and reference (pointers) | ||
+ - | unary sign operator | ||
4 | (type) | type casting | Right-to-left |
5 | .* ->* | pointer-to-member | Left-to-right |
6 | * / % | multiplicative | Left-to-right |
7 | + - | additive | Left-to-right |
8 | << >> | shift | Left-to-right |
9 | < > <= >= | relational | Left-to-right |
10 | == != | equality | Left-to-right |
11 | & | bitwise AND | Left-to-right |
12 | ^ | bitwise XOR | Left-to-right |
13 | | | bitwise OR | Left-to-right |
14 | && | logical AND | Left-to-right |
15 | || | logical OR | Left-to-right |
16 | ?: | conditional | Right-to-left |
17 | = *= /= %= += -= >>= <<= &= ^= |= | assignment | Right-to-left |
18 | , | comma | Left-to-right |
流操作符
<< >>