C++运算符重载:让自定义类操作更便捷
立即解锁
发布时间: 2025-08-17 01:59:01 阅读量: 1 订阅数: 4 

### C++ 运算符重载:让自定义类操作更便捷
#### 1. 运算符重载简介
在 C++ 中,内置类型(如 `int`)可以方便地使用各种运算符,例如加法(`+`)和乘法(`*`)。以下是一个简单的示例:
```cpp
int x = 17, y = 12, z;
z = x * (y + 5);
```
在这个例子中,C++ 编译器知道如何处理 `*` 和 `+` 运算符,最终 `z` 的值为 289。
如果使用自定义类来实现相同的功能,代码会变得更加复杂。例如,使用 `Number` 类:
```cpp
Number x(17);
Number y(12);
Number z, temp;
temp = y.add(5);
z = x.multiply(temp);
```
虽然结果仍然是 289,但代码明显更长且更复杂。为了简化操作,C++ 提供了运算符重载技术,允许自定义类使用运算符进行操作。
运算符重载定义了特定运算符与类对象一起使用时的行为。几乎所有 C++ 运算符都可以被重载,使用运算符编写的表达式更易于阅读和理解。
#### 2. 计数器类示例
为了更好地理解运算符重载,我们从一个简单的 `Counter` 类开始。以下是 `Counter.cpp` 的完整代码:
```cpp
#include <iostream>
class Counter
{
public:
Counter();
~Counter(){}
int getValue() const { return value; }
void setValue(int x) { value = x; }
private:
int value;
};
Counter::Counter():
value(0)
{}
int main()
{
Counter c;
std::cout << "The value of c is " << c.getValue()
<< std::endl;
return 0;
}
```
这个程序创建了一个 `Counter` 对象并显示其当前值,输出为 `The value of c is 0`。然而,目前这个 `Counter` 对象不能像内置 `int` 类型那样进行递增、递减、加法、赋值等操作,也不能方便地显示其值。接下来我们将解决这些问题。
#### 3. 编写递增方法
运算符重载为自定义类提供了原本缺失的功能。在类中重载运算符最常见的方法是使用成员函数,其函数声明形式如下:
```cpp
returnType operatorsymbol(parameter list)
{
// 重载成员函数的主体
}
```
函数名由 `operator` 后跟要定义的运算符组成,例如 `+` 或 `++`。`returnType` 是函数的返回类型,参数列表根据运算符的不同可以包含零个、一个或两个参数。
以下是 `Counter2.cpp` 的代码,展示了如何重载递增运算符 `++`:
```cpp
#include <iostream>
class Counter
{
public:
Counter();
~Counter(){}
int getValue() const { return value; }
void setValue(int x) { value = x; }
void increment() { ++value; }
const Counter& operator++();
private:
int value;
};
Counter::Counter():
value(0)
{}
const Counter& Counter::operator++()
{
++value;
return *this;
}
int main()
{
Counter c;
std::cout << "The value of c is " << c.getValue()
<< std::endl;
c.increment();
std::cout << "The value of c is " << c.getValue()
<< std::endl;
++c;
std::cout << "The value of c is " << c.getValue()
<< std::endl;
Counter a = ++c;
std::cout << "The value of a: " << a.getValue();
std::cout << " and c: " << c.getValue() << std::endl;
return 0;
}
```
这个程序多次递增 `Counter` 对象并创建了另一个对象,输出结果如下:
```
The value of c is 0
The value of c is 1
The value of c is 2
The value of a: 3 and c: 3
```
在第 35 行,`++c` 调用了 `operator++` 成员函数,该函数递增成员变量 `value` 并返回当前对象的引用。由于返回的是当前对象的引用,因此可以将其赋值给变量 `a`。
需要注意的是,如果 `Counter` 对象分配了内存,可能需要重写拷贝构造函数。在这个例子中,默认的拷贝构造函数就可以正常工作。返回的是 `Counter` 引用,避免了创建额外的临时对象,并且由于函数不会改变对象的值,所以返回的是 `const` 引用。
#### 4. 重载后缀运算符
前面的示例使用了前缀版本的 `++` 递增运算符,那么如何重载后缀运算符呢?由于前缀和后缀运算符都是 `++`,仅通过函数名无法区分它们。
为了重载后缀运算符,需要在 `operator++()` 成员函数中添加一个 `int` 类型的参数,这个整数不会被使用,只是作为一个标志,表示该函数定义的是后缀运算符。
以下是 `Counter3.cpp` 的代码,展示了如何重载前缀和后缀运算符:
```cpp
#include <iostream>
class Counter
{
public:
Counter();
~Counter(){}
int getValue() const { return value; }
void setValue(int x) { value = x; }
const Counter& operator++(); // 前缀
const Counter operator++(int); // 后缀
private:
int value;
};
Counter::Counter():
value(0)
{}
const Counter& Counter::operator++() // 前缀
{
++value;
return *this;
}
const Counter Counter::operator++(int) // 后缀
{
Counter temp(*this);
++value;
return temp;
}
int main()
{
Counter c;
std::cout << "The value of c is " << c.getValue()
<< std::endl;
c++;
std::cout << "The value of c is " << c.getValue()
<< std::endl;
++c;
std::cout << "The value of c is " << c.getValue()
<< std::endl;
Counter a = ++c;
std::cout << "The value of a: " << a.getValue();
std::cout << " and c: " << c.getValue() << std::endl;
a = c++;
std::cout << "The value of a: " << a.getValue();
std::cout << " and c: " << c.getValue() << std::endl;
return 0;
}
```
这个程序重载了前缀和后缀递增
0
0
复制全文
相关推荐










