操作符Operators

赋值

Assignment (=)


式子从右到左执行。 right-to-left rule).


#include <iostream>
using namespace std;


class A;
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

aba && b
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

|| OPERATOR

aba || b
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

!true        // evaluates to false
!false       // evaluates to true. 

条件三位操作符

Conditional operator ( ? )


a ? b : c;

逗号操作符

Comma operator ( , )


a = (b=3, b+2);


位操作符

Bitwise Operators ( &, |, ^, ~, <<, >> )


operatorasm equivalentdescription
&ANDBitwise AND
|ORBitwise Inclusive OR
^XORBitwise Exclusive OR
~NOTUnary complement (bit inversion)
<<SHLShift Left
>>SHRShift Right

显示类型转换

int i;
float f = 3.14;
i = (int) f;
i = int(f);

长度

sizeof()



其他操作符

Other operators



运算子优先级

Precedence of operators


LevelOperatorDescriptionGrouping
1::scopeLeft-to-right
2() [] . -> ++ -- dynamic_cast static_cast reinterpret_cast const_cast typeidpostfixLeft-to-right
3++ -- ~ ! sizeof new deleteunary (prefix)Right-to-left
* &indirection and reference (pointers)
+ -unary sign operator
4(type)type castingRight-to-left
5.* ->*pointer-to-memberLeft-to-right
6* / %multiplicativeLeft-to-right
7+ -additiveLeft-to-right
8<< >>shiftLeft-to-right
9< > <= >=relationalLeft-to-right
10== !=equalityLeft-to-right
11&bitwise ANDLeft-to-right
12^bitwise XORLeft-to-right
13|bitwise ORLeft-to-right
14&&logical ANDLeft-to-right
15||logical ORLeft-to-right
16?:conditionalRight-to-left
17= *= /= %= += -= >>= <<= &= ^= |=assignmentRight-to-left
18,commaLeft-to-right

流操作符

<< >>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值