服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++运算符重载的详细讲解

C++运算符重载的详细讲解

2021-10-29 14:52Thrush''''s note C/C++

这篇文章主要给大家介绍了关于C++运算符重载的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

加号运算符重载

对于内置数据类型,编译器知道如何运算

但是对于自己封装的类,编译器无法进行运算

这时可以通过自己定义运算符重载进行运算

operator+

通过成员函数重载+号

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
using namespace std;
class Person
{
public:
 int m_a;
 int m_b;
 //通过成员函数实现重载
 Person operator+ (Person &p)
 {
 //创建一个临时变量
 Person temp;
 temp.m_a = this->m_a + p.m_a;
 temp.m_b = this->m_b + p.m_b;
 return temp;
 }
};
void test01()
{
 Person p1;
 p1.m_a = 66;
 p1.m_b = 44;
 Person p2;
 p2.m_a = 6;
 p2.m_b = 4;
 Person p3;
 //通过函数原型调用
 p3 = p1.operator+(p2);
 //简便调用
 //p3 = p1 + p2;
 cout << "p3.m_a:" << p3.m_a << endl;
 cout << "p3.m_b:" << p3.m_b << endl;
}
 
int main()
{
 test01();
 system("pause");
 return 0;
}

注意两种调用方式

通过函数原型调用

p3 = p1.operator+(p2);

简便调用

p3 = p1 + p2;

通过全局函数重载+号

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
using namespace std;
class Person
{
public:
 int m_a;
 int m_b;
};
//通过全局函数实现重载
Person operator+ (Person& p1, Person& p2)
{
 //创建一个临时变量
 Person temp;
 temp.m_a = p1.m_a + p2.m_a;
 temp.m_b = p1.m_b + p2.m_b;
 return temp;
}
void test01()
{
 Person p1;
 p1.m_a = 66;
 p1.m_b = 44;
 Person p2;
 p2.m_a = 6;
 p2.m_b = 4;
 Person p3;
 //函数原型调用
 p3 = operator+(p1,p2);
 //简便调用
 //p3 = p1 + p2;
 cout << "p3.m_a:" << p3.m_a << endl;
 cout << "p3.m_b:" << p3.m_b << endl;
}
int main()
{
 test01();
 system("pause");
 return 0;
}

注意两种调用方式

通过函数原型调用

p3 = operator+(p1,p2);

简便调用

p3 = p1 + p2;

运算符重载发生函数重载

运算符重载可以发生函数重载:Person+int等等

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
using namespace std;
class Person
{
public:
 int m_a;
 int m_b;
};
//通过全局函数实现重载
Person operator+ (Person& p1, int num)
{
 //创建一个临时变量
 Person temp;
 temp.m_a = p1.m_a + num;
 temp.m_b = p1.m_b + num;
 return temp;
}
void test01()
{
 Person p1;
 p1.m_a = 66;
 p1.m_b = 44;
 Person p2;
 p2.m_a = 6;
 p2.m_b = 4;
 Person p3;
 //函数原型调用
 //p3 = operator+(p1,55);
 //简便调用
 p3 = p1 + 55;
 cout << "p3.m_a:" << p3.m_a << endl;
 cout << "p3.m_b:" << p3.m_b << endl;
}
int main()
{
 test01();
 system("pause");
 return 0;
}

调用方法和定义方法与上面相同,不再多余赘述

总结

1、系统内置数据类型的表达式不可改变

2、不要滥用运算符重载

左移运算符

不利用成员函数重载左移运算符

没有具体演示,因为报错,我也没写出来

下面通过全局函数实现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
using namespace std;
class Person
{
public:
 int m_a;
 int m_b;
 
};
ostream& operator<<(ostream& cout,Person&p)
{
 cout << "p.m_a=" <<p. m_a << " p.m_b=" <<p. m_b << endl;
 return cout;
}
void test01()
{
 Person p1;
 p1.m_a = 44;
 p1.m_b = 66;
 cout << p1 << endl;
}
int main()
{
 test01();
 system("pause");
 return 0;
}

因为要实现链式,实现追加,所以返回值必须是ostream

总结

配合友元实现自定义输出类型

递增运算符重载

递增运算符重载

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
using namespace std;
class myInt
{
 friend ostream& operator<<(ostream& cout, myInt num);
public:
 myInt()
 {
 this->m_a = 0;
 }
 //前置++运算符重载
 myInt& operator++()//返回引用是为了一直对一个数据进行递增,否则函数默认返回一个新的数
 {
 //先进行++
 m_a++;
 //然后返回自身
 return *this;
 }
 //后置++运算符重载
 myInt operator++(int)//int表示占位参数,用于区分前置后置参数
 {
 //先记录当前的值
 myInt temp=*this;
 //再递增
 m_a++;
 //然后返回记录的值
 return temp;
 }
private:
 int m_a;
};
//左移运算符重载
ostream& operator<<(ostream& cout, myInt num)
{
 cout << num.m_a;
 return cout;
}
void test01()
{
 myInt myint;
 cout << myint << endl;
 cout << ++myint << endl;
 cout << myint << endl;
 
}
int main()
{
 test01();
 system("pause");
 return 0;
}

赋值运算符重载

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
using namespace std;
class Person
{
public:
 Person(int num)//将数据开辟到堆区
 {
 m_a = new int(num);
 }
 ~Person()
 {
 if (m_a != NULL)
 {
  delete m_a;
  m_a = NULL;
 }
 }
 //重载赋值运算符
 Person& operator=(Person &p)//返回值用Person返回本身,可执行连等
 {
 //先判断是否有属性在堆区,如果有先释放干净
 if (m_a != NULL)
 {
  delete m_a;
  m_a = NULL;
 }
 m_a = new int(*p.m_a);
 return *this;
 }
 int* m_a;
 
};
void test01()
{
 Person p1(18);
 Person p2(209);
 Person p3(9);
 p2 = p1 = p3;
 cout << *p1.m_a << endl;
 cout << *p2.m_a << endl;
 cout << *p3.m_a << endl;
}
int main()
{
 test01();
 system("pause");
 return 0;
}

关系运算符重载

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
 
 Person(string name, int age)
 {
 this->age = age;
 this->name = name;
 }
 bool operator==(Person& p)
 {
 if (this->age == p.age && this->name == p.name)
 {
  return true;
 }
 else {
  return false;
 }
 }
 bool operator!=(Person& p)
 {
 if (this->age == p.age && this->name == p.name)
 {
  return false;
 }
 else {
  return true;
 }
 }
 string name;
 int age;
 
 
};
void test01()
{
 Person p1("gouride", 19);
 Person p2("gouride", 19);
 if (p1 == p2) {
 cout << "p1和p2相同" << endl;
 }
 else {
 cout << "p1和p2不相同" << endl;
 }
 if (p1 != p2) {
 cout << "p1和p2不相同" << endl;
 }
 else {
 cout << "p1和p2相同" << endl;
 }
}
int main()
{
 test01();
 system("pause");
 return 0;
}

函数调用重载

仿函数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
using namespace std;
#include<string>
class Myprint
{
public:
 void operator()(string name)
 {
 cout << name << endl;
 }
 int operator()(int a,int b)
 {
 return a + b;
 }
};
void test01()
{
 
 Myprint myprint;
 myprint("测试");
 //匿名对象调用
 cout << Myprint()(4,6) << endl;
}
int main()
{
 test01();
 system("pause");
 return 0;
}

总结

到此这篇关于C++运算符重载的文章就介绍到这了,更多相关C++运算符重载内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/thrush/p/14615937.html

延伸 · 阅读

精彩推荐
  • C/C++C语言main函数的三种形式实例详解

    C语言main函数的三种形式实例详解

    这篇文章主要介绍了 C语言main函数的三种形式实例详解的相关资料,需要的朋友可以参考下...

    ieearth6912021-05-16
  • C/C++关于C语言中E-R图的详解

    关于C语言中E-R图的详解

    今天小编就为大家分享一篇关于关于C语言中E-R图的详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看...

    Struggler095962021-07-12
  • C/C++深入C++拷贝构造函数的总结详解

    深入C++拷贝构造函数的总结详解

    本篇文章是对C++中拷贝构造函数进行了总结与介绍。需要的朋友参考下...

    C++教程网5182020-11-30
  • C/C++c/c++实现获取域名的IP地址

    c/c++实现获取域名的IP地址

    本文给大家汇总介绍了使用c/c++实现获取域名的IP地址的几种方法以及这些方法的核心函数gethostbyname的详细用法,非常的实用,有需要的小伙伴可以参考下...

    C++教程网10262021-03-16
  • C/C++C语言实现双人五子棋游戏

    C语言实现双人五子棋游戏

    这篇文章主要为大家详细介绍了C语言实现双人五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    两片空白7312021-11-12
  • C/C++OpenCV实现拼接图像的简单方法

    OpenCV实现拼接图像的简单方法

    这篇文章主要为大家详细介绍了OpenCV实现拼接图像的简单方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    iteye_183805102021-07-29
  • C/C++使用C++制作简单的web服务器(续)

    使用C++制作简单的web服务器(续)

    本文承接上文《使用C++制作简单的web服务器》,把web服务器做的功能稍微强大些,主要增加的功能是从文件中读取网页并返回给客户端,而不是把网页代码...

    C++教程网5492021-02-22
  • C/C++c/c++内存分配大小实例讲解

    c/c++内存分配大小实例讲解

    在本篇文章里小编给大家整理了一篇关于c/c++内存分配大小实例讲解内容,有需要的朋友们可以跟着学习参考下。...

    jihite5172022-02-22