C++ 的运算符

 

Precedence Operator Description Example Overloadable Associativity
1::Scope resolution operatorClass::age = 2;nonone
2()
()
[]
->
.
++
--
const_cast
dynamic_cast
static_cast
reinterpret_cast
typeid
Function call
Member initalization
Array access
Member access from a pointer
Member access from an object
Post-increment
Post-decrement
Special cast
Special cast
Special cast
Special cast
Runtime type information
isdigit('1')
c_tor(int x, int y) : _x(x), _y(y*10){};
array[4] = 2;
ptr->age = 34;
obj.age = 34;
for( int i = 0; i < 10; i++ ) cout << i;
for( int i = 10; i > 0; i-- ) cout << i;
const_cast<type_to>(type_from);
dynamic_cast<type_to>(type_from);
static_cast<type_to>(type_from);
reinterpret_cast<type_to>(type_from);
cout « typeid(type).name();
yes
yes
yes
yes
no
yes
yes
no
no
no
no
no
left to right
3!
not
~
compl
++
--
-
+
*
&
new
new []
delete
delete []
(type)
sizeof
Logical negation
Alternate spelling for !
Bitwise complement
Alternate spelling for ~
Pre-increment
Pre-decrement
Unary minus
Unary plus
Dereference
Address of
Dynamic memory allocation
Dynamic memory allocation of array
Deallocating the memory
Deallocating the memory of array
Cast to a given type
Return size of an object or type
if( !done ) …

flags = ~flags;

for( i = 0; i < 10; ++i ) cout << i;
for( i = 10; i > 0; --i ) cout << i;
int i = -1;
int i = +1;
int data = *intPtr;
int *intPtr = &data;
long *pVar = new long;
MyClass *ptr = new MyClass(args);
delete pVar;
delete [] array;
int i = (int) floatNum;//int size = sizeof(float);
yes

yes

yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
no
right to left
4->*
.*
Member pointer selector
Member object selector
ptr->*var = 24;
obj.*var = 24;
yes
no
left to right
5*
/
%
Multiplication
Division
Modulus
int i = 2 * 4;
float f = 10.0 / 3.0;
int rem = 4 % 3;
yes
yes
yes
left to right
6+
-
Addition
Subtraction
int i = 2 + 3;
int i = 5 - 1;
yes
yes
left to right
7<<
>>
Bitwise shift left
Bitwise shift right
int flags = 33 << 1;
int flags = 33 >> 1;
yes
yes
left to right
8<
<=
>
>=
Comparison less-than
Comparison less-than-or-equal-to
Comparison greater-than
Comparison greater-than-or-equal-to
if( i < 42 ) …
if( i <= 42 ) ...
if( i > 42 ) …
if( i >= 42 ) ...
yes
yes
yes
yes
left to right
9==
eq
!=
not_eq
Comparison equal-to
Alternate spelling for ==
Comparison not-equal-to
Alternate spelling for !=
if( i == 42 ) ...

if( i != 42 ) …
yes
-
yes
left to right
10&
bitand
Bitwise AND
Alternate spelling for &
flags = flags & 42;
yes
left to right
11^
xor
Bitwise exclusive OR (XOR)
Alternate spelling for ^
flags = flags ^ 42;
yes
left to right
12|
bitor
Bitwise inclusive (normal) OR
Alternate spelling for |
flags = flags | 42;
yes
left to right
13&&
and
Logical AND
Alternate spelling for &&
if( conditionA && conditionB ) …
yes
left to right
14||
or
Logical OR
Alternate spelling for ||
if( conditionA || conditionB ) ...
yes
left to right
15? :Ternary conditional (if-then-else)int i = (a > b) ? a : b;noright to left
16=
+=
-=
*=
/=
%=
&=
and_eq
^=
xor_eq
|=
or_eq
<<=
>>=
Assignment operator
Increment and assign
Decrement and assign
Multiply and assign
Divide and assign
Modulo and assign
Bitwise AND and assign
Alternate spelling for &=
Bitwise exclusive or (XOR) and assign
Alternate spelling for ^=
Bitwise normal OR and assign
Alternate spelling for |=
Bitwise shift left and assign
Bitwise shift right and assign
int a = b;
a += 3;
b -= 4;
a *= 5;
a /= 2;
a %= 3;
flags &= new_flags;

flags ^= new_flags;

flags |= new_flags;

flags <<= 2;
flags >>= 2;
yes
yes
yes
yes
yes
yes
yes

yes

yes

yes
yes
right to left
17throwthrow exceptionthrow EClass(“Message”);no
18,Sequential evaluation operatorfor( i = 0, j = 0; i < 10; i++, j++ ) …yesleft to right

<think>嗯,用户想了解C++中的运算符类型、功能和使用方法,特别是关于运算符重载的部分。首先,我需要整理C++中的基本运算符类型,比如算术、关系、逻辑、位运算、赋值、其他运算符等。然后,每个类型的功能和使用方法,比如加减乘除的作用,如何用在代码里。 接下来是运算符重载的部分,用户提供的引用资料里提到运算符重载的概念,比如流插入和提取运算符的重载,还有如何自己重载运算符。需要解释为什么需要重载,比如让自定义类型像内置类型一样使用运算符,比如复数相加的例子。 然后要说明哪些运算符可以重载,哪些不能,比如作用域运算符“::”就不能重载。还要讲重载的两种方式:成员函数和全局函数,特别是像<<和>>这样的运算符通常用全局函数重载,并且需要友元声明。 还要提到一些注意事项,比如保持运算符的原有语义,避免混淆,比如重载+运算符不应该做减法操作。同时,特殊运算符如赋值运算符=的重载需要注意深拷贝和浅拷贝的问题,尤其是在有动态内存分配的情况下。 用户可能还关心如何实际编写重载运算符的代码,比如示例中的复数类重载+运算符,或者重载<<输出运算符。需要确保示例代码正确,符合C++语法,并且注释清晰。 另外,用户提供的引用里提到了流运算符的重载需要包含头文件stream,以及使用命名空间std,这些细节要准确,避免误导用户。还有前置和后置++的区别,参数中的占位符int,这些也需要在回答中说明。 可能用户没有直接问,但相关的问题包括运算符重载的限制、何时应该使用成员函数 vs 全局函数、重载运算符的返回类型选择等。在生成相关问题时,可以包括这些方面,比如运算符重载的限制、成员函数和全局函数的区别、如何重载赋值运算符处理深拷贝等。 最后,确保回答结构清晰,分点列出运算符类型,每个类型的功能和使用方法,再详细讲解运算符重载,结合示例代码,并自然地添加引用标识。检查LaTeX格式是否正确,行内公式用$包围,独立公式用$$,代码块用正确语法高亮。</think>以下是对C++运算符的详细说明,涵盖类型、功能及使用方法,结合运算符重载机制的分析: --- ### 一、C++运算符基础分类 | 运算符类型 | 运算符示例 | 功能描述 | |------------------|---------------------------|-----------------------------| | **算术运算符** | `+`, `-`, `*`, `/`, `%` | 数值计算 | | **关系运算符** | `==`, `!=`, `<`, `>`, `<=`| 比较操作,返回布尔值 | | **逻辑运算符** | `&&`, `||`, `!` | 布尔逻辑运算 | | **位运算符** | `&`, `|`, `^`, `~`, `<<`, `>>` | 二进制位操作 | | **赋值运算符** | `=`, `+=`, `-=`, `*=`, `%=` | 赋值或组合赋值操作 | | **其他运算符** | `::`, `.*`, `->*`, `new`, `delete` | 作用域、成员指针、动态内存管理 | --- ### 二、运算符重载机制 #### 1. **核心功能** - **赋予运算符新含义**:使运算符能操作自定义类型(如类或结构体),例如实现复数加法: ```cpp Complex a(1,2), b(3,4); Complex c = a + b; // 通过重载+实现复数相加 ``` - **增强代码可读性**:使表达式更接近数学形式[^1]。 #### 2. **重载方法** - **成员函数形式**(隐式调用左操作数): ```cpp class Complex { public: Complex operator+(const Complex& rhs) { return Complex(real + rhs.real, imag + rhs.imag); } }; ``` - **全局函数形式**(需声明为友元,常用于流运算符): ```cpp ostream& operator<<(ostream& os, const Complex& c) { os << c.real << "+" << c.imag << "i"; return os; } ``` #### 3. **可重载运算符列表** - **允许重载**:`+`, `-`, `<<`, `>>`, `[]`, `()`, `new`, `delete` 等47种运算符 - **禁止重载**:`::`, `.*`, `.`, `?:`, `sizeof`[^3] --- ### 三、关键运算符重载示例 #### 1. **输入/输出运算符(<< 和 >>)** ```cpp // 重载>>实现自定义类型输入 istream& operator>>(istream& is, Complex& c) { is >> c.real >> c.imag; return is; } ``` *注:必须返回流引用以支持链式调用*[^1] #### 2. **下标运算符[]** ```cpp class Vector { int* data; public: int& operator[](int index) { return data[index]; } }; ``` #### 3. **自增运算符++** ```cpp class Counter { int value; public: // 前置++ Counter& operator++() { ++value; return *this; } // 后置++ Counter operator++(int) { // int为占位参数 Counter temp = *this; ++value; return temp; } }; ``` --- ### 四、运算符重载原则 1. **语义一致性**:保持运算符原有含义(如`+`应实现加法而非减法) 2. **类型安全**:避免隐式类型转换导致的歧义 3. **效率优化**:返回引用减少临时对象创建(如赋值运算符) 4. **特殊处理**:赋值运算符需处理自赋值问题[^3] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值