Linux C++ 017-运算符重载

2024-04-04 10:04
文章标签 c++ linux 重载 运算符 017

本文主要是介绍Linux C++ 017-运算符重载,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Linux C++ 017-运算符重载

本节关键字:Linux、C++、运算符重载、匿名函数
相关库函数:

运算符重载的概念

对已有的运算符重新进行定义,赋予其另外一种功能,以适应不同的数据类型* 运算符重载可以发生函数重载
* 对于内置的数据类型的表达式的运算符是不可能改变的
* 不要滥用运算符重载

运算符重载的作用

(1) 加号运算符(operator+):实现两个自定义数据类型相加的运算(2) 左移运算符(operator<<):可以输出自定义数据类型(3) 递增运算符(operator++):实现自己的整型数据递增* 前置递增返回的是引用* 后置递增返回的是值(4) 赋值运算符(operator=):C++编译器至少给一个类添加4个函数:1.默认构造函数2.默认析构函数3.默认拷贝函数4.赋值运算符 operator= ,对属性进行值拷贝* 如果类中有属性指向堆区,做赋值操作时会出现深浅拷贝问题(5) 关系运算符(operator> operator< operator>= operator<= operator== operator!=):可以让两个自定义类型对象进行对比操作(6) 函数调用运算符(operator()):1.函数调用运算符 () 也可以重载2.由于重载后使用的方式非常像函数的调用,因此成为仿函数3.仿函数没有固定写法,非常灵活

加号运算符重载

class Person
{
public: Person(int a, int b);
public:Person operator+(Person &p);int m_A;int m_B;
};
Person::Person(int a, int b)
{m_A = a;m_B = b;
}
//通过成员函数重载运算符:
Person 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;
}
Person p3 = p1.operator+(p2);//本质调用
//简化为
Person p3 = p1 + p2;//通过全局函数重载运算符:
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;
}
Person p3 = operator+(p1,p2);//本质调用
//简化为
Person p3 = p1 + p2;

左移运算符重载

class Person
{friend ostream operator<<(ostream &cout, Person &p);
public: Person();Person operator<<(Person &p);private:	int m_A;int m_B;
};
Person::Person(int a, int b)
{m_A = a;m_B = b;
}
//不会利用成员函数重载左移运算符,因为无法实现cout在左边
//通过全局函数重载运算符:
ostream operator<<(ostream &cout, Person &p)
{cout << "m_A = " << p.m_A << "m_B = " << p.m_B;return cout;
}
void test()
{Person p(10,10);cout << p << "hello" << endl;
}

递增运算符重载

class MyInteger
{friend ostream operator<<(ostream cout, MyInteger myint);
public:MyInteger();//前置MyInteger& operator++();//后置MyInteger& operator++(int);//int 为占位参数,可以区分前置和后置递增
private:int m_Num;
};
MyInteger::MyInteger()
{m_Num = 0;
}
//通过成员函数实现后置递增
MyInteger& MyInteger::operator++()
{m_Num++;return *this;
}
//通过成员函数实现前置递增
MyInteger MyInteger::operator++(int)
{//先记录当时的结果MyInteger temp = *this;//递增m_Num++;//最后将记录的结果返回return temp;
}
//通过全局函数重载左移运算符
ostream operator<<(ostream cout, MyInteger myint)
{cout << myint.m_Num;return cout;
}
void test()
{MyInteger myint;cout ++(++myint) << endl;cout << myint << endl;
}

赋值运算符重载

class Person
{
public:Person(int age);~Person();Person& operator=(Person &p);
private:int *m_Age;
};
Person::Person(int age)
{m_Age = new int(age);
}
Person::~Person()
{if(m_Age != NULL){delete m_Age;m_Age = NULL;}
}
Person& Person::operator=(Person &p)
{//应该先判断是否有属性在堆区,若有要先释放干净,然后深拷贝if(m_Age != NULL){delete m_Age;m_Age = NULL;}//深拷贝m_Age = new int(*p.m_Age);return *this;
}void test()
{Person p1(18);Person p2(20);p2 = p1;cout << "p1的年龄为:" << *p1.m_Age << endl;cout << "p1的年龄为:" << *p2.m_Age << endl;
}

关系运算符重载

class Person
{
public:Person(string name, int age);bool operator==(Person& p);bool operator!=(Person& p);
private:strig m_Name;int m_Age;
};
Person::Person(string name, int age)
{m_Name = name;m_Age = age;
}
bool Person::operator==(Person& p)
{if(this->m_Name == p.m_Name && this->m_Age == p.m_Age){return true;}return false;
}
bool Person::operator!=(Person& p)
{if(this->m_Name != p.m_Name || this->m_Age != p.m_Age){return true;}return false;
}void test()
{Person p1("TOm", 18);Person p2("Tom", 18);if(p1 == p2){cout << "p1 和 p2 相等" << endl;}else{cout << "p1 和 p2 不相等" << endl;}if(p1 != p2){cout << "p1 和 p2 不相等" << endl;}else{cout << "p1 和 p2 相等" << endl;}
}

函数调用运算符重载

class MyPrint
{
public:MyPrint();void operator()(string test);
private:};
MyPrint::MyPrint()
{}
void MyPrint::operator()(string test)
{cout << test << endl;
}
void MyPrint02(string test)
{cout << test << endl;
}
void test()
{MyPrint myp;myp.MyPrint("hello");MyPrint02("你好");
}

匿名函数

class MyAdd
{
public:int operator()(int num1, int num2);
};
int MyAdd::operator()(int num1, int num2)
{return num1+num2;
}
void test2()
{MyAdd myadd;int ret = myadd(100,100);cout << "ret = " << ret << endl;//匿名函数对象cout << MyAdd()(100, 100) << endl;
}

这篇关于Linux C++ 017-运算符重载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/875490

相关文章

VScode连接远程Linux服务器环境配置图文教程

《VScode连接远程Linux服务器环境配置图文教程》:本文主要介绍如何安装和配置VSCode,包括安装步骤、环境配置(如汉化包、远程SSH连接)、语言包安装(如C/C++插件)等,文中给出了详... 目录一、安装vscode二、环境配置1.中文汉化包2.安装remote-ssh,用于远程连接2.1安装2

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

Linux中shell解析脚本的通配符、元字符、转义符说明

《Linux中shell解析脚本的通配符、元字符、转义符说明》:本文主要介绍shell通配符、元字符、转义符以及shell解析脚本的过程,通配符用于路径扩展,元字符用于多命令分割,转义符用于将特殊... 目录一、linux shell通配符(wildcard)二、shell元字符(特殊字符 Meta)三、s

Linux之软件包管理器yum详解

《Linux之软件包管理器yum详解》文章介绍了现代类Unix操作系统中软件包管理和包存储库的工作原理,以及如何使用包管理器如yum来安装、更新和卸载软件,文章还介绍了如何配置yum源,更新系统软件包... 目录软件包yumyum语法yum常用命令yum源配置文件介绍更新yum源查看已经安装软件的方法总结软

linux报错INFO:task xxxxxx:634 blocked for more than 120 seconds.三种解决方式

《linux报错INFO:taskxxxxxx:634blockedformorethan120seconds.三种解决方式》文章描述了一个Linux最小系统运行时出现的“hung_ta... 目录1.问题描述2.解决办法2.1 缩小文件系统缓存大小2.2 修改系统IO调度策略2.3 取消120秒时间限制3

Linux alias的三种使用场景方式

《Linuxalias的三种使用场景方式》文章介绍了Linux中`alias`命令的三种使用场景:临时别名、用户级别别名和系统级别别名,临时别名仅在当前终端有效,用户级别别名在当前用户下所有终端有效... 目录linux alias三种使用场景一次性适用于当前用户全局生效,所有用户都可调用删除总结Linux

Linux:alias如何设置永久生效

《Linux:alias如何设置永久生效》在Linux中设置别名永久生效的步骤包括:在/root/.bashrc文件中配置别名,保存并退出,然后使用source命令(或点命令)使配置立即生效,这样,别... 目录linux:alias设置永久生效步骤保存退出后功能总结Linux:alias设置永久生效步骤

Linux使用fdisk进行磁盘的相关操作

《Linux使用fdisk进行磁盘的相关操作》fdisk命令是Linux中用于管理磁盘分区的强大文本实用程序,这篇文章主要为大家详细介绍了如何使用fdisk进行磁盘的相关操作,需要的可以了解下... 目录简介基本语法示例用法列出所有分区查看指定磁盘的区分管理指定的磁盘进入交互式模式创建一个新的分区删除一个存

Linux使用dd命令来复制和转换数据的操作方法

《Linux使用dd命令来复制和转换数据的操作方法》Linux中的dd命令是一个功能强大的数据复制和转换实用程序,它以较低级别运行,通常用于创建可启动的USB驱动器、克隆磁盘和生成随机数据等任务,本文... 目录简介功能和能力语法常用选项示例用法基础用法创建可启动www.chinasem.cn的 USB 驱动

高效管理你的Linux系统: Debian操作系统常用命令指南

《高效管理你的Linux系统:Debian操作系统常用命令指南》在Debian操作系统中,了解和掌握常用命令对于提高工作效率和系统管理至关重要,本文将详细介绍Debian的常用命令,帮助读者更好地使... Debian是一个流行的linux发行版,它以其稳定性、强大的软件包管理和丰富的社区资源而闻名。在使用