C++ 运算符重载是指对已有运算符重新定义,以使它们能够处理用户自定义的数据类型。重载运算符可以使自定义类型的操作更加自然和直观。以下部分是详细的讲解,包括所有类型的重载、注意点、常用的C++运算符重载代码示例以及运行结果解释。
C++运算符重载详解及常用的运算符重载代码示例
1. 运算符重载类型
C++中可以重载的运算符主要分为以下几类:
- 算术运算符:
+
,-
,*
,/
,%
- 关系运算符:
==
,!=
,<
,>
,<=
,>=
- 逻辑运算符:
&&
,||
,!
- 位运算符:
&
,|
,^
,~
,<<
,>>
- 赋值运算符:
=
,+=
,-=
,*=
,/=
,%=
等 - 增量和减量运算符:
++
,--
- 下标运算符:
[]
- 函数调用运算符:
()
- 指针运算符:
*
,->
- 输入输出运算符:
<<
,>>
2. 注意点
- 不能重载的运算符:
::
,.
,.*
,?:
- 重载运算符必须是类的成员函数或友元函数。
- 一元运算符通常作为成员函数重载,二元运算符可以作为成员或友元函数重载。
- 对称性运算符(如
==
,+
)有时需要友元函数以保持操作数交换的对称性。 - 重载的运算符的优先级和结合性无法改变。
3. 常用的C++运算符重载代码示例
(1) 算术运算符重载
#include <iostream>
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);
}
// 重载 << 运算符用于输出
friend std::ostream& operator << (std::ostream& out, const Complex& c)
{
out << c.real << " + " << c.imag << "i";
return out;
}
};
int main()
{
Complex c1(3.0, 4.0), c2(1.0, 2.0);
Complex c3 = c1 + c2;
std::cout << "c1 + c2 = " << c3 << std::endl;
return 0;
}
解释:
Complex
类中重载了+
运算符,实现了复数相加。<<
运算符被重载以便于输出复数格式。
输出结果:
c1 + c2 = 4 + 6i
(2) 关系运算符重载
#include <iostream>
class Complex
{
private:
double real, imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
// 重载 == 运算符
bool operator == (const Complex& other) const
{
return (real == other.real) && (imag == other.imag);
}
};
int main()
{
Complex c1(3.0, 4.0), c2(3.0, 4.0), c3(1.0, 2.0);
if (c1 == c2)
{
std::cout << "c1 and c2 are equal." << std::endl;
}
else
{
std::cout << "c1 and c2 are not equal." << std::endl;
}
if (c1 == c3)
{
std::cout << "c1 and c3 are equal." << std::endl;
}
else
{
std::cout << "c1 and c3 are not equal." << std::endl;
}
return 0;
}
解释:
Complex
类中重载了==
运算符,用于比较两个复数是否相等。
输出结果:
c1 and c2 are equal.
c1 and c3 are not equal.
(3) 下标运算符重载
#include <iostream>
class Array
{
private:
int* arr;
int size;
public:
Array(int s) : size(s)
{
arr = new int[size];
}
~Array()
{
delete[] arr;
}
// 重载 [] 运算符
int& operator[](int index)
{
if (index >= 0 && index < size)
{
return arr[index];
}
else
{
std::cerr << "Index out of bounds" << std::endl;
exit(1);
}
}
};
int main()
{
Array a(10);
a[0] = 10;
a[1] = 20;
std::cout << "a[0] = " << a[0] << std::endl;
std::cout << "a[1] = " << a[1] << std::endl;
return 0;
}
解释:
Array
类中重载了[]
运算符,用于数组元素的访问。
输出结果:
a[0] = 10
a[1] = 20
(4) 赋值运算符重载
#include <iostream>
class Complex
{
private:
double real, imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
// 重载 = 运算符
Complex& operator = (const Complex& other)
{
if (this == &other) return *this;
real = other.real;
imag = other.imag;
return *this;
}
// 重载 << 运算符用于输出
friend std::ostream& operator << (std::ostream& out, const Complex& c)
{
out << c.real << " + " << c.imag << "i";
return out;
}
};
int main()
{
Complex c1(3.0, 4.0), c2;
c2 = c1;
std::cout << "c2 = " << c2 << std::endl;
return 0;
}
解释:
Complex
类中重载了=
运算符,实现对象赋值。
输出结果:
c2 = 3 + 4i
这些示例详细介绍了不同类型的运算符重载以及在C++中重载运算符时需要注意的要点。这些重载使得自定义类型能够自然地使用运算符,从而增强代码的可读性和可维护性。