Some knowledge points about operators

1. You can use keywords instead of logical operators:

#define debug qDebug()<<
int main(int argc, char *argv[])
{
    debug (true and false);
}

 2. C++ does not clearly stipulate the evaluation order of expressions. It cannot be assumed that expressions are evaluated from left to right. In order to avoid strange running results, avoid reading and writing the same object in the same expression at the same time (such as avoiding writing v[i] = i++ this kind of code).

3. The comma operator, logical AND (&&), logical OR (||) stipulate that the operand on their left side is evaluated first.

  • The general form of the comma operator is: expression 1, expression 2. The evaluation process is to evaluate the values ​​of two expressions separately, and use the value of expression 2 as the value of the entire comma expression. (Such as: y = (x = a + b),(b + c);)
  • && will only evaluate the value on the right when the evaluation result of the operand on the left is true
  • ||The value on the right side will be evaluated only when the evaluation result on the left side is false
C++ operator precedence table
priority Operator Description Associativity
1 :: Scope resolution From left to right
2 ++   -- Suffix auto-increment/suffix auto-decrement
() brackets
[] Array subscript
. Member selection (object)
−> Member selection (pointer)
3 ++   -- Prefix increment/prefix decrement Right to left
+   − Add/subtract
!   ~ Logical negation/bitwise negation
(type) Forced type conversion
* Take the value pointed to by the pointer
& So and so's address
sizeof So-and-so's size
new, new[] Dynamic memory allocation / dynamic array memory allocation
delete, delete[] Dynamic memory release/dynamic array memory release
4 .*   ->* Member object selection/member pointer selection From left to right
5 *   /   % Multiplication/Division/Remainder
6 +   − Plus/minus
7 <<   >> Bit shift left/Bit shift right
8 <   <= Less than / less than or equal to
>   >= Greater than/greater than or equal
9 ==   != Equal/not equal
10 & Bitwise and
11 ^ Bitwise XOR
12 | Bitwise or
13 && AND operation
14 || OR operation
15 ?: Ternary operator Right to left
16 = Assignment
+=   −= Assignment after addition/Assignment after subtraction
*=   /=   %= Assignment after multiplication/Assignment after division/Assignment after taking the remainder
<<=   >>= Bit shift assignment to the left/Bit shift assignment to the right
&=   ^=   |= Assignment after bitwise AND operation/Bitwise exclusive OR assignment after bitwise operation/Assignment after bitwise OR operation
17 throw Throw an exception
18 , comma From left to right

 4、一些运算符可以重载:

5、重载函数形式是:operator后面加运算符,a+b相当于a.operator+(b)

6、预置含义:++a等价于a+=1和a=a+1,但不适用于此规则, 编译器不会根据A::operator+()和A::operator=()生成::operator+=()的定义。一个类默认带有赋值运算、取地址运算、顺序(逗号)运算的含义的,可以选择去除这些含义:

#define debug qDebug()<<
struct ceshi
{
    int frist;
    int second;
    ceshi(int one = 0,int two = 0):frist{one},second{two}
    {
    }
    void operator =(const ceshi&) = delete;
    void operator &() = delete;
    void operator ,(const ceshi&) = delete;
};

int main(int argc, char *argv[])
{
    ceshi a(4,5);
    ceshi b = a;
    a = b;
    &a;
    a,b;
}

注:ceshi b = a;是调用(拷贝/移动)构造函数而不是operator=()。

Guess you like

Origin blog.csdn.net/kenfan1647/article/details/113817131