运算符重载是C++中通过函数重载为类对象定义运算符行为的机制,可使用成员函数(如+、-、=、[])或友元函数(如)实现,需遵循原有语义并注意特殊规则,使自定义类型操作更直观自然。

在C++中,运算符重载是面向对象编程的重要特性之一,它允许我们为自定义类型(如类或结构体)重新定义已有的运算符行为,使对象之间的操作更直观、自然。比如可以让两个对象使用+直接相加。
什么是运算符重载
运算符重载的本质是函数重载。通过定义特定的函数,让原本只能用于基本数据类型的运算符也能用于类对象。例如,我们可以重载+、-、==、等运算符。
如何实现运算符重载
运算符重载可以通过两种方式实现:成员函数和友元函数。选择哪种方式取决于具体运算符和使用场景。
1. 成员函数方式重载当运算符的操作数中第一个操作数是当前类对象时,可以使用成员函数重载。这类运算符包括+、-、+=、==等二元运算符,以及++、--等一元运算符。
成员函数的参数个数比运算符所需的操作数少一个,因为隐含的this指针代表第一个操作数。
示例:重载+运算符
class Complex {
private:
double real, imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
// 成员函数重载 + 运算符
Complex operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
void show() const {
cout <font color="#0000FF"><strong>2. 友元函数方式重载</strong></font><p>当需要重载的运算符涉及不同类型操作数,或左操作数不是当前类对象(如<code>cout ),就必须使用友元函数。典型例子是输入输出运算符<code>和<code>>></code>。</code></code></p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill10691" title="Livestream Script Generator"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20e695c2374.jpg" alt="Livestream Script Generator" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill10691" title="Livestream Script Generator" class="overflowclass">Livestream Script Generator</a>
<p class="overflowclass">直播带货脚本:话术、节奏、互动、促单。适用于主播、电商运营、MCN机构。</p>
</div>
<a rel="nofollow" href="/xiazai/skill10691" title="Livestream Script Generator" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><p>示例:重载<code>运算符</code></p><pre class="brush:php;toolbar:false;">
class Complex {
// ...
friend ostream& operator<p>这样就可以直接使用<code>cout 输出对象内容。</code></p><font color="#0000FF"><strong>3. 重载赋值运算符=</strong></font><p>如果类中包含动态分配的资源(如指针),必须显式重载赋值运算符,以防止浅拷贝带来的问题。</p><p>赋值运算符应返回<code>*this</code>的引用,并检查自赋值。</p><pre class="brush:php;toolbar:false;">
Complex& operator=(const Complex& other) {
if (this == &other) return *this; // 自赋值检查
real = other.real;
imag = other.imag;
return *this;
}
4. 重载下标运算符[]常用于实现类似数组的访问方式,一般作为成员函数实现,提供非常量和常量两个版本。
class MyArray {
int data[10];
public:
int& operator[](int index) {
return data[index];
}
const int& operator[](int index) const {
return data[index];
}
};
注意事项与规则
运算符重载有一些限制和最佳实践:
- 不能创建新的运算符,只能重载已有运算符
- 以下运算符不能重载:
::、.*、.、?:、sizeof - 重载运算符应尽量保持原有语义,避免滥用
- 对于对称性操作(如
+),优先考虑友元函数,以便支持自动类型转换 - 某些运算符(如
=、[]、()、->)必须是成员函数
基本上就这些。掌握这些方法后,你可以根据实际需求灵活地为类定义直观的操作方式。关键是要理解每种重载方式的适用场景和语法细节。
C++免费学习笔记(深入):立即使用
在学习笔记中,你将探索 C++ 的入门与实战技巧!









