重载运算符和重载函数
C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载。
重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是它们的参数列表和定义(实现)不相同。
当您调用一个重载函数或重载运算符时,编译器通过把您所使用的参数类型与定义中的参数类型进行比较,决定选用最合适的定义。选择最合适的重载函数或重载运算符的过程,称为重载决策。

C++ 中的函数重载
在同一个作用域内,可以声明几个功能类似的同名函数,但是这些同名函数的形式参数(指参数的个数、类型或者顺序)必须不同。您不能仅通过返回类型的不同来重载函数。
下面的实例代码1中,同名函数 print() 被用于输出不同的数据类型:

#include <iostream>
using namespace std; class printData
{
public:
void print(int i) {
cout << "整数为: " << i << endl;
}
void print(double f) {
cout << "浮点数为: " << f << endl;
}
void print(char c[]) {
cout << "字符串为: " << c << endl;
}
}; int main(void)
{
printData pd;
// 输出整数
pd.print();
// 输出浮点数
pd.print(500.263);
// 输出字符串
char c[] = "Hello C++";
pd.print(c);
return ;
}

编译输出:

kevin@kevin-virtual-machine:~/Documents/Test/C++/seven$ ./chongzai1
整数为:
浮点数为: 500.263
字符串为: Hello C++

C++ 中的运算符重载
您可以重定义或重载大部分 C++ 内置的运算符。这样,您就能使用自定义类型的运算符。
重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。

Box operator+(const Box&);

声明加法运算符用于把两个 Box 对象相加,返回最终的 Box 对象。大多数的重载运算符可被定义为普通的非成员函数或者被定义为类成员函数。如果我们定义上面的函数为类的非成员函数,那么我们需要为每次操作传递两个参数,如下所示:

Box operator+(const Box&, const Box&);

下面的实例使用成员函数演示了运算符重载的概念。在这里,对象作为参数进行传递,对象的属性使用 this 运算符进行访问,如下实例代码2所示:

#include <iostream>
using namespace std; class Box
{
public:
double getVolume(void)
{
return length * breadth * height;
} void setLength( double len )
{
length = len;
} void setBreadth( double bre )
{
breadth = bre;
} void setHeight( double hei )
{
height = hei;
}
// 重载 + 运算符,用于把两个 Box 对象相加
Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
double length; // 长度
double breadth; // 宽度
double height; // 高度
};
// 程序的主函数
int main()
{
Box Box1; // 声明 Box1,类型为 Box
Box Box2; // 声明 Box2,类型为 Box
Box Box3; // 声明 Box3,类型为 Box
double volume = 0.0; // 把体积存储在该变量中 // Box1 详述
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0); // Box2 详述
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0); // Box1 的体积
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl; // Box2 的体积
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl; // 把两个对象相加,得到 Box3
Box3 = Box1 + Box2; // Box3 的体积
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl; return ;
}

编译输出:

kevin@kevin-virtual-machine:~/Documents/Test/C++/seven$ ./chongzai2
Volume of Box1 :
Volume of Box2 :
Volume of Box3 :

可重载运算符/不可重载运算符
下面是可重载的运算符列表:

双目算术运算符  + (加),-(减),*(乘),/(除),% (取模)
关系运算符  ==(等于),!= (不等于),< (小于),> (大于>,<=(小于等于),>=(大于等于)
逻辑运算符  ||(逻辑或),&&(逻辑与),!(逻辑非)
单目运算符  + (正),-(负),*(指针),&(取地址)
自增自减运算符  ++(自增),--(自减)
位运算符  | (按位或),& (按位与),~(按位取反),^(按位异或),,<< (左移),>>(右移)
赋值运算符  =, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>=
空间申请与释放  new, delete, new[ ] , delete[]
其他运算符  ()(函数调用),->(成员访问),,(逗号),[](下标)

下面是不可重载的运算符列表:

. 成员访问运算符
.*, ->* 成员指针访问运算符
:: 域运算符
sizeof 长度运算符
?: 条件运算符
# 预处理符号

运算符重载实例
下面提供了各种运算符重载的实例,帮助您更好地理解重载的概念。

1. 一元运算符重载

一元运算符只对一个操作数进行操作,下面是一元运算符的实例:
a. 递增运算符( ++ )和递减运算符( -- )
b. 一元减运算符,即负号( - )
c. 逻辑非运算符( ! )
一元运算符通常出现在它们所操作的对象的左边,比如 !obj、-obj 和 ++obj,但有时它们也可以作为后缀,比如 obj++ 或 obj--。
下面的实例代码3演示了如何重载一元减运算符( - )。

#include <iostream>
using namespace std; class Distance
{
private:
int feet; // 0 到无穷
int inches; // 0 到 12
public:
// 所需的构造函数
Distance(){
feet = ;
inches = ;
}
Distance(int f, int i){
feet = f;
inches = i;
cout << "construct func feet: " << feet << " I: " << inches << endl;
}
// 显示距离的方法
void displayDistance()
{
cout << "F: " << feet << " I:" << inches <<endl;
}
// 重载负运算符( - )
Distance operator- ()
{
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
};
int main()
{
Distance D1(, ), D2(-, );
cout << "Debug 1" << endl;
-D1; // 取相反数
cout << "Debug 2" << endl;
D1.displayDistance(); // 距离 D1
cout << "Debug 3" << endl;
-D2; // 取相反数
D2.displayDistance(); // 距离 D2 return ;
}

编译输出:

kevin@kevin-virtual-machine:~/Documents/Test/C++/seven$ g++ operator1.cpp -o operator1
kevin@kevin-virtual-machine:~/Documents/Test/C++/seven$ ./operator1
construct func feet: I:
construct func feet: - I:
Debug
construct func feet: - I: -
Debug
F: - I:-
Debug
construct func feet: I: -
F: I:-

二元运算符重载
二元运算符需要两个参数,下面是二元运算符的实例。我们平常使用的加运算符( + )、减运算符( - )、乘运算符( * )和除运算符( / )都属于二元运算符。就像加(+)运算符。
下面的实例演示了如何重载加运算符( + )。类似地,您也可以尝试重载减运算符( - )和除运算符( / )。

实例参考上面实例代码2。

当 2 个对象相加时是没有顺序要求的,但要重载 + 让其与一个数字相加则有顺序要求,可以通过加一个友元函数使另一个顺序的输入合法。

实例代码4

#include<iostream>
using namespace std;
class A
{
private:
int a;
public:
A();
A(int n);
A operator+(const A & obj);
A operator+(const int b);
friend A operator+(const int b, A obj);
void display();
} ;
A::A()
{
a=;
}
A::A(int n)//构造函数
{
a=n;
}
A A::operator +(const A& obj)//重载+号用于 对象相加
{
return this->a+obj.a;
}
A A::operator+(const int b)//重载+号用于 对象与数相加
{
return A(a+b);
}
A operator+(const int b, A obj)
{
return obj+b;//友元函数调用第二个重载+的成员函数 相当于 obj.operator+(b);
}
void A::display()
{
cout<<a<<endl;
}
int main ()
{
A a1();
A a2();
A a3,a4,a5;
a1.display();
a2.display();
int m=;
a3=a1+a2;//可以交换顺序,相当月a3=a1.operator+(a2);
a3.display();
a4=a1+m;//因为加了个友元函数所以也可以交换顺序了。
a4.display();
a5=m+a1;
a5.display();
}

编译输出:

kevin@kevin-virtual-machine:~/Documents/Test/C++/seven$ ./operator2

关系运算符重载

C++ 语言支持各种关系运算符( < 、 > 、 <= 、 >= 、 == 等等),它们可用于比较 C++ 内置的数据类型。
您可以重载任何一个关系运算符,重载后的关系运算符可用于比较类的对象。
下面的实例代码5演示了如何重载 < 运算符,类似地,您也可以尝试重载其他的关系运算符。

#include <iostream>
using namespace std; class Distance
{
private:
int feet; // 0 到无穷
int inches; // 0 到 12
public:
// 所需的构造函数
Distance(){
feet = ;
inches = ;
}
Distance(int f, int i){
feet = f;
inches = i;
}
// 显示距离的方法
void displayDistance()
{
cout << "F: " << feet << " I:" << inches <<endl;
}
// 重载负运算符( - )
Distance operator- ()
{
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
// 重载小于运算符( < )
bool operator <(const Distance& d)
{
if(feet < d.feet)
{
return true;
}
if(feet == d.feet && inches < d.inches)
{
return true;
}
return false;
}
};
int main()
{
Distance D1(, ), D2(, ); if( D1 < D2 )
{
cout << "D1 is less than D2 " << endl;
}
else
{
cout << "D2 is less than D1 " << endl;
}
return ;
}

编译输出:

kevin@kevin-virtual-machine:~/Documents/Test/C++/seven$ ./operator3
D2 is less than D1

输入/输出运算符重载

C++ 能够使用流提取运算符 >> 和流插入运算符 << 来输入和输出内置的数据类型。您可以重载流提取运算符和流插入运算符来操作对象等用户自定义的数据类型。
在这里,有一点很重要,我们需要把运算符重载函数声明为类的友元函数,这样我们就能不用创建对象而直接调用函数。
下面的实例代码6演示了如何重载提取运算符 >> 和插入运算符 <<。

#include <iostream>
using namespace std; class Distance
{
private:
int feet; // 0 到无穷
int inches; // 0 到 12
public:
// 所需的构造函数
Distance(){
feet = ;
inches = ;
}
Distance(int f, int i){
feet = f;
inches = i;
}
friend ostream &operator<<( ostream &output,
const Distance &D )
{
output << "F : " << D.feet << " I : " << D.inches;
return output;
} friend istream &operator>>( istream &input, Distance &D )
{
input >> D.feet >> D.inches;
return input;
}
};
int main()
{
Distance D1(, ), D2(, ), D3; cout << "Enter the value of object : " << endl;
cin >> D3;
cout << "First Distance : " << D1 << endl;
cout << "Second Distance :" << D2 << endl;
cout << "Third Distance :" << D3 << endl; return ;
}

编译输出:

kevin@kevin-virtual-machine:~/Documents/Test/C++/seven$ ./operator4
Enter the value of object : First Distance : F : I :
Second Distance :F : I :
Third Distance :F : I :

++ 和 -- 运算符重载
递增运算符( ++ )和递减运算符( -- )是 C++ 语言中两个重要的一元运算符。
下面的实例代码7演示了如何重载递增运算符( ++ ),包括前缀和后缀两种用法。类似地,您也可以尝试重载递减运算符( -- )。

#include <iostream>
using namespace std; class Time
{
private:
int hours; // 0 到 23
int minutes; // 0 到 59
public:
// 所需的构造函数
Time(){
hours = ;
minutes = ;
}
Time(int h, int m){
hours = h;
minutes = m;
}
// 显示时间的方法
void displayTime()
{
cout << "H: " << hours << " M:" << minutes <<endl;
}
// 重载前缀递增运算符( ++ )
Time operator++ ()
{
++minutes; // 对象加 1
if(minutes >= )
{
++hours;
minutes -= ;
}
return Time(hours, minutes);
}
// 重载后缀递增运算符( ++ )
Time operator++( int )
{
// 保存原始值
Time T(hours, minutes);
// 对象加 1
++minutes;
if(minutes >= )
{
++hours;
minutes -= ;
}
// 返回旧的原始值
return T;
}
};
int main()
{
Time T1(, ), T2(,); ++T1; // T1 加 1
T1.displayTime(); // 显示 T1
++T1; // T1 再加 1
T1.displayTime(); // 显示 T1 T2++; // T2 加 1
T2.displayTime(); // 显示 T2
T2++; // T2 再加 1
T2.displayTime(); // 显示 T2
return ;
}

编译输出:

kevin@kevin-virtual-machine:~/Documents/Test/C++/seven$ ./operator5
H: M:
H: M:
H: M:
H: M:

赋值运算符重载
就像其他运算符一样,您可以重载赋值运算符( = ),用于创建一个对象,比如拷贝构造函数。
下面的实例代码8演示了如何重载赋值运算符。

#include <iostream>
using namespace std; class Distance
{
private:
int feet; // 0 到无穷
int inches; // 0 到 12
public:
// 所需的构造函数
Distance(){
feet = ;
inches = ;
}
Distance(int f, int i){
feet = f;
inches = i;
}
void operator=(const Distance &D )
{
feet = D.feet;
inches = D.inches;
}
// 显示距离的方法
void displayDistance()
{
cout << "F: " << feet << " I:" << inches << endl;
} };
int main()
{
Distance D1(, ), D2(, ); cout << "First Distance : ";
D1.displayDistance();
cout << "Second Distance :";
D2.displayDistance(); // 使用赋值运算符
D1 = D2;
cout << "First Distance :";
D1.displayDistance(); return ;
}

编译输出:

kevin@kevin-virtual-machine:~/Documents/Test/C++/seven$ ./operator6
First Distance : F: I:
Second Distance :F: I:
First Distance :F: I:

函数调用运算符 () 重载
函数调用运算符 () 可以被重载用于类的对象。当重载 () 时,您不是创造了一种新的调用函数的方式,相反地,这是创建一个可以传递任意数目参数的运算符函数。
下面的实例代码9演示了如何重载函数调用运算符 ()。

#include <iostream>
using namespace std; class Distance
{
private:
int feet; // 0 到无穷
int inches; // 0 到 12
public:
// 所需的构造函数
Distance(){
feet = ;
inches = ;
}
Distance(int f, int i){
feet = f;
inches = i;
}
// 重载函数调用运算符
Distance operator()(int a, int b, int c)
{
Distance D;
// 进行随机计算
D.feet = a + c + ;
D.inches = b + c + ;
return D;
}
// 显示距离的方法
void displayDistance()
{
cout << "F: " << feet << " I:" << inches << endl;
} };
int main()
{
Distance D1(, ), D2; cout << "First Distance : ";
D1.displayDistance(); D2 = D1(, , ); // invoke operator()
cout << "Second Distance :";
D2.displayDistance(); return ;
}

编译输出:

kevin@kevin-virtual-machine:~/Documents/Test/C++/seven$ ./operator7
First Distance : F: I:
Second Distance :F: I:

下标运算符 [] 重载
下标操作符 [] 通常用于访问数组元素。重载该运算符用于增强操作 C++ 数组的功能。
下面的实例代码10演示了如何重载下标运算符 []。

#include <iostream>
using namespace std;
const int SIZE = ; class safearay
{
private:
int arr[SIZE];
public:
safearay()
{
register int i;
for(i = ; i < SIZE; i++)
{
arr[i] = i;
}
}
int& operator[](int i)
{
if( i > SIZE )
{
cout << "索引超过最大值" <<endl;
// 返回第一个元素
return arr[];
}
return arr[i];
}
};
int main()
{
safearay A; cout << "A[2] 的值为 : " << A[] <<endl;
cout << "A[5] 的值为 : " << A[]<<endl;
cout << "A[12] 的值为 : " << A[]<<endl; return ;
}

编译输出:

kevin@kevin-virtual-machine:~/Documents/Test/C++/seven$ ./operator8
A[] 的值为 :
A[] 的值为 :
索引超过最大值
A[] 的值为 :

类成员访问运算符 -> 重载
类成员访问运算符( -> )可以被重载,但它较为麻烦。它被定义用于为一个类赋予"指针"行为。运算符 -> 必须是一个成员函数。如果使用了 -> 运算符,返回类型必须是指针或者是类的对象。
运算符 -> 通常与指针引用运算符 * 结合使用,用于实现"智能指针"的功能。这些指针是行为与正常指针相似的对象,唯一不同的是,当您通过指针访问对象时,它们会执行其他的任务。比如,当指针销毁时,或者当指针指向另一个对象时,会自动删除对象。
间接引用运算符 -> 可被定义为一个一元后缀运算符。也就是说,给出一个类:

class Ptr{
//...
X * operator->();
};

类 Ptr 的对象可用于访问类 X 的成员,使用方式与指针的用法十分相似。例如:

void f(Ptr p )
{
p->m = ; // (p.operator->())->m = 10
}

语句 p->m 被解释为 (p.operator->())->m。同样地,下面的实例代码11演示了如何重载类成员访问运算符 ->。

#include <iostream>
#include <vector>
using namespace std; // 假设一个实际的类
class Obj {
static int i, j;
public:
void f() const { cout << i++ << endl; }
void g() const { cout << j++ << endl; }
}; // 静态成员定义
int Obj::i = ;
int Obj::j = ; // 为上面的类实现一个容器
class ObjContainer {
vector<Obj*> a;
public:
void add(Obj* obj)
{
a.push_back(obj); // 调用向量的标准方法
}
friend class SmartPointer;
}; // 实现智能指针,用于访问类 Obj 的成员
class SmartPointer {
ObjContainer oc;
int index;
public:
SmartPointer(ObjContainer& objc)
{
oc = objc;
index = ;
}
// 返回值表示列表结束
bool operator++() // 前缀版本
{
if(index >= oc.a.size() - ) return false;
if(oc.a[++index] == ) return false;
return true;
}
bool operator++(int) // 后缀版本
{
return operator++();
}
// 重载运算符 ->
Obj* operator->() const
{
if(!oc.a[index])
{
cout << "Zero value";
return (Obj*);
}
return oc.a[index];
}
}; int main() {
const int sz = ;
Obj o[sz];
ObjContainer oc;
for(int i = ; i < sz; i++)
{
oc.add(&o[i]);
}
SmartPointer sp(oc); // 创建一个迭代器
do {
sp->f(); // 智能指针调用
sp->g();
} while(sp++);
return ;
}

编译输出:

kevin@kevin-virtual-machine:~/Documents/Test/C++/seven$ ./operator9

C++解析七-重载运算符和重载函数的更多相关文章

  1. C++ 重载运算符和重载函数

    C++ 重载运算符和重载函数 C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载. 重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是 ...

  2. 吴裕雄--天生自然C++语言学习笔记:C++ 重载运算符和重载函数

    C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载. 重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是它们的参数列表和定义(实现)不 ...

  3. C++ Primer : 第十四章 : 重载运算与类型转换之重载运算符

    重载前须知 重载运算符是特殊的函数,它们的名字由operator和其后要重载的运算符号共同组成. 因为重载运算符时函数, 因此它包含返回值.参数列表和函数体. 对于重载运算符是成员函数时, 它的第一个 ...

  4. C++ 重载运算符简单举例

    我们可以重定义或重载大部分 C++ 内置的运算符.这样,就能使用自定义类型的运算符. 重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的.与其他函数一 ...

  5. C++ 重载运算符(详)

    C++ 重载运算符 C 重载运算符 一重载函数 1例程 2备注 二重载运算符 11 二元运算符重载 11 一元运算符重载 111 -- 2备注 3 特殊运算符重载 31 号运算符 32 下标运算符 3 ...

  6. c++中重载运算符

    重载运算符 1,成员函数运算符 运算符重载为类的成员函数一般格式如下 <函数类型> operator <运算符> (参数表) {函数体} 调用成员函数运算符如下 <对象名 ...

  7. 关于对STL容器重载运算符的问题

    注意:下文中vector数组指的是每个数组元素都是一个vector的数组 大部分有序STL容器都重载了小于号,且比较方式是对容器中元素进行字典序比较. 此时如果你写\(bool\ operator&l ...

  8. C++学习27 用全局函数重载运算符

    运算符重载函数既可以声明为类的成员函数,也可以声明为所有类之外的全局函数. 运算符重载函数作为类的成员函数 将运算符重载函数声明为类的成员函数时,二元运算符的参数只有一个,一元运算符不需要参数.之所以 ...

  9. # c++运算符重载之 前置++, 后置++, 负号运算符, 类型转换函数, 以及输入输出运算符

    c++运算符重载之 前置++, 后置++, 负号运算符, 类型转换函数, 以及输入输出运算符 标签(空格分隔): c++ 前言 我在c++学习的过程中, 对这几个不太常见的运算符重载不太会写.出现了很 ...

随机推荐

  1. eclipse中启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误

    原因1:给定目录下jvm.dll不存在. 对策:(1)重新安装jre或者jdk并配置好环境变量.(2)copy一个jvm.dll放在该目录下. 原因2:eclipse的版本与jre或者jdk版本不一致 ...

  2. Python 闭包(Closure)

    Python  闭包 (Closure) 这里介绍一下python 的闭包 基本概念 闭包(closure)是函数式编程的重要的语法结构. 函数式编程的一个特点就是,允许把函数本身作为参数传入另一个函 ...

  3. appium --log-timestamp > appium.log

     appium --log-timestamp > appium.log 

  4. 细胞迁移 | cell migration

    一些基本概念: intracellular biochemical signaling pathways:胞内生化信号通路 extracellular mechanical cues: 胞外机械信号 ...

  5. vscode插件之C/C++

    目录 1.给C/C++调试器配置launch.json 2.配置VS Code的调试行为 3.配置目标应用 4.自定义GDB或者LLDB 5.调试dump(转储)文件 6.远程调试或者本地服务器上调试 ...

  6. 第一阶段——站立会议总结DAY02

    1.昨天做了什么:做出了一个大概的界面(APP的个人中心界面),有用户名信息和按钮. 2.今天准备做什么:因为仅是完成在页面上显示文字,而显示个人信息的化,想要做成微信的个人中心辣个样子. ①就是文字 ...

  7. 4月24 php基础及函数的应用

    PHP是一种被广泛认可应用,运行在服务端的脚本语言,PHP需要安转PHP应用程序服务器去解释执行,是用来协助Web服务器工作的编程语言,因此所写的任何代码只有在www文件下的才能实施预览.PHP是一种 ...

  8. springboot使用redis的配置application.yml连接池以及存取

    第一步:添加pom文件依赖: <!-- redis --> <dependency> <groupId>redis.clients</groupId> ...

  9. 成功解决You are using pip version 9.0.1, however version 9.0.3 is available. You should consider upgra

    解决问题 You are using pip version 9.0.3, however version 10.0.1 is available.You should consider upgrad ...

  10. uva-11324-SCC+dp

    https://vjudge.net/problem/UVA-11324 给出一幅有向图,问最大能找到多少个节点,使得这些节点中任意两个节点之间都至少有一条可达路径. 找出SCC后缩点求权重最大路即可 ...