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

 更新时间:2021年04月07日 10:00:50   作者:Thrush''''s note  
这篇文章主要给大家介绍了关于C++运算符重载的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

加号运算符重载

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

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

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

operator+

通过成员函数重载+号

#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;

通过全局函数重载+号

#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等等

#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、不要滥用运算符重载

左移运算符

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

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

下面通过全局函数实现

#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

总结

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

递增运算符重载

递增运算符重载

#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;
}

赋值运算符重载

#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;
}

关系运算符重载

#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;
}

函数调用重载

仿函数

#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++运算符重载内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C 语言环境设置详细讲解

    C 语言环境设置详细讲解

    本文主要介绍C 语言环境设置,在不同的系统平台上,C语言的环境设置不同,这里帮大家整理了Liunx, UNIX,Windows 上安装C语言环境,有开始学习C语言的朋友可以参考下
    2016-08-08
  • C语言实现简单计算器

    C语言实现简单计算器

    这篇文章主要为大家详细介绍了C语言实现简单计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05
  • 用C编写一个送给女朋友的情人节小程序 可爱!

    用C编写一个送给女朋友的情人节小程序 可爱!

    非常可爱的情人节小程序!文章为大家分享了用C编写一个送给女朋友的小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • C++模拟实现stack和Queue的操作示例

    C++模拟实现stack和Queue的操作示例

    这篇文章主要介绍了C++模拟实现stack和Queue的操作示例,文中通过代码示例给大家介绍的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-06-06
  • C++位操作的常见用法小结

    C++位操作的常见用法小结

    这篇文章主要介绍了C++位操作的常见用法,包括了判断奇偶、交换两个数、正负转换、求绝对值等,需要的朋友可以参考下
    2014-09-09
  • VSCode (Visual Studio Code) V1.43.0下载并设置成中文语言的方法

    VSCode (Visual Studio Code) V1.43.0下载并设置成中文语言的方法

    Visual Studio Code是一款免费开源的现代化轻量级代码编辑器,支持语法高亮、智能代码补全、自定义热键、括号匹配、代码片段、代码对比 Diff、GIT 等特性,这篇文章主要介绍了VSCode (Visual Studio Code) V1.43.0下载并设置成中文语言,需要的朋友可以参考下
    2020-03-03
  • C++11的新特性简单汇总介绍 (二)

    C++11的新特性简单汇总介绍 (二)

    最近学习了C++11的新特性,将学习内容整理下来以巩固记忆,这里分享给大家,希望对大家学习C++11能够有所帮助
    2016-07-07
  • C++ TensorflowLite模型验证的过程详解

    C++ TensorflowLite模型验证的过程详解

    这篇文章给大家介绍了C++ TensorflowLite模型验证的过程,测试代码,主要是RunInference()和read_file(),详细操作过程跟随小编一起看看吧
    2021-08-08
  • 详解C语言函数返回值解析

    详解C语言函数返回值解析

    这篇文章主要介绍了详解C语言函数返回值解析的相关资料,需要的朋友可以参考下
    2017-06-06
  • C++ sqlite3数据库配置使用教程

    C++ sqlite3数据库配置使用教程

    SQLite 是一种嵌入式的关系型数据库管理系统,它是一个开源项目,已经被广泛应用于各种应用程序和操作系统中,这篇文章主要介绍了C++ sqlite3数据库配置使用,需要的朋友可以参考下
    2023-08-08

最新评论