c++中运算符重载(加号运算,左移运算,前置后置++运算符,赋值运算,关系运算,函数运算)
运算符重载注意
- 重载的运算符要易读
- 内置的数据类型的表达式的运算符是不可以改变的
- 不要重载
&&
和| |
运算符 =
,[]
和->
运算符只能通过成员函数进行重载<<
和>>
只能通过全局函数配合友元函数进行重载
加号运算符重载
如果想让自定义数据类型 进行**+**运算,那么就需要重载 **+**运算符
在成员函数 或者 全局函数里 重写 一个**+**运算符的函数
函数名operate+(){}
运算符重载可以不停重载,接着重载
#include<iostream>using namespace std;class Person{public:Person(){}Person(int a, int b) :m_A(a), m_B(b){}//+号运算符重载 成员函数/*Person operator+(Person & p){Person tmp;tmp.m_A= this->m_A + p.m_A;tmp.m_B = this->m_B + p.m_B;return tmp;}*/int m_A;int m_B;};//利用全局函数,进行+号运算符的重载Person operator+(Person &p1, Person &p2) //二元{Person tmp;tmp.m_A = p1.m_A + p2.m_A;tmp.m_B = p1.m_B + p2.m_B;return tmp;}//函数重载 附带运算符再次重载Person operator+(Person &p1, int a) //二元{Person tmp;tmp.m_A = p1.m_A + a;tmp.m_B = p1.m_B + a;return tmp;}void test01(){Person p1(10, 10);Person p2(10, 10);Person p3 = p1 + p2; //p1+p2 从什么表达式转变的? p1.operator(p2) operator(p1,p2)Person p4 = p1 + 10;//重载的版本cout << "p3的m_A" << p3.m_A << endl << "p3的m_B" << p3.m_B << endl;}int main(){test01();system("pause");return 0;}
左移运算符重载
不要随意乱用符号重载
内置数据类型的运算符不可以重载
cout<< 直接对Person自定义数据类型 进行输出
写到全局函数中 ostream& operator<<(ostream & cout,Person & p1){}
如果重载时想访问p1的私有成员,那么全局函数要做Person的友元函数
#include<iostream>using namespace std;class Person{friend ostream & operator <<(ostream &cout, Person&p1);public:Person(){}Person(int a, int b){this->m_A = a;this->m_B = b;}/*void operator<<() 重载左移运算符不可以写到成员函数中{}*/private:int m_A;int m_B;};//cout是属于ostream类 //如果向访问私有成员,就要用友元函数ostream & operator <<(ostream &cout, Person&p1) //第一个参数cout 第二个参数p1{cout << "m_A=" << p1.m_A << "m_B=" << p1.m_B;return cout;}void test01(){Person p1(10, 10);cout << p1<<endl;}int main(){test01();system("pause");return 0;}
前置,后置 ++运算符重载
自己实现
int
类型MyIntege
内部维护
int
数据MyInteger myint
myint ++ 后置 ++myint前置
重载++运算符 operator++()前置 operator++(int )后置
前置理念 先++ 后返回自身 后置理念 先保存原有值 内部++ 返回临时数据
#include<iostream>using namespace std;class MyInteger{friend ostream & operator<<(ostream &cout, MyInteger& myInt);public:MyInteger(){m_Num = 0;}//前置++重载//返回引用MyInteger & operator++(){this->m_Num++;return *this;}//用占位参数区分前置和后置//后置++重载//返回值,MyInteger operator++(int){//先保存目前的数据MyInteger tmp = *this;//实际数据++m_Num++;//返回++前的数值return tmp;}//所以前置++好,因为返回引用,少一份开销int m_Num;};ostream & operator<<(ostream &cout, MyInteger& myInt){cout << myInt.m_Num;return cout;}void test01(){MyInteger myInt;//前置++值cout << ++myInt << endl;//后置++值cout << myInt++ << endl;//本身的值cout << myInt << endl;}int main(){test01();system("pause");return 0;}
赋值运算符重载
系统默认给类提供 赋值运算符写法 是简单值拷贝
导致如果类中有指向堆区的指针,就可能出现深浅拷贝的问题
所以要重载=运算符
链式编程return *this
#define _CRT_SECURE_NO_WARNINGS#include<iostream>using namespace std;class Person{public:Person(int a){this->m_A = a;}int m_A;};void test01(){Person p1(10);Person p2(0);p2 = p1;cout << "p2的m_A" << p2.m_A << endl;}class Person2{public:Person2(char * name){this->pName = new char[strlen(name) + 1];strcpy(this->pName, name);}//类中重载 =赋值运算符Person2 & operator=(const Person2 &p){//判断如果原来已经堆区有内容,先释放if (this->pName != NULL){delete[]this->pName;this->pName = NULL;}this->pName = new char[strlen(p.pName) + 1];strcpy(this->pName, p.pName);return *this;}~Person2(){if (this->pName != NULL){delete[]this->pName;this->pName = NULL;}}char * pName;};void test02(){Person2 p1("狗蛋");Person2 p2("狗剩");Person2 p3("");p3=p2 = p1;cout << p2.pName << endl;cout << p3.pName << endl;cout << p2.pName << endl;int a = 10;int b = 20;int c;c = a = b; //都是20cout << a << " " << b << " " << c << endl;}int main(){test02();system("pause");return 0;}
关系运算符重载
#include<iostream>#include<string>using namespace std;class Person{public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}bool operator==(Person &p){if (this->m_Name == p.m_Name&&this->m_Age == p.m_Age){return true;}return false;}bool operator!=(Person &p){if (this->m_Name == p.m_Name&&this->m_Age == p.m_Age){return false;}return true;}string m_Name;int m_Age;};void test01(){Person p1("小明", 10);Person p2("小强", 15);Person p3("小强", 15);/*int a = 10;int b = 10;if (a == b){cout << "a,b相等" << endl;}*/if (p1 == p2){cout << "p1和p2相等" << endl;}else{cout << "不相等" << endl;}if (p3 == p2){cout << "p3和p2相等" << endl;}else{cout << "不相等" << endl;}if (p1 != p2){cout << "p1和p2不相等" << endl;}else{cout << "相等" << endl;}}int main(){test01();system("pause");return 0;}
函数调用运算符重载
#include<iostream>#include<string>using namespace std;// ()重载class MyPrint{public:void operator()(string text){cout << text << endl;}};void test01(){MyPrint myPrint;myPrint("hello world"); //仿函数}class MyAdd{public:int operator()(int v1,int v2){return v1 + v2;}};void test02(){/*MyAdd myAdd;cout << myAdd(1, 1) << endl;*/cout << MyAdd()(1, 1) << endl; //匿名对象}int main(){//test01();test02();system("pause");return 0;}
c++中运算符重载(加号运算,左移运算,前置后置++运算符,赋值运算,关系运算,函数运算)相关推荐
- 几个特殊的运算符重载(前置\后置++、前置\后置--、<<、>>)
几个特殊的运算符重载 概念 运算符重载是一个非常重要的概念,在运算符重载中我们可以重新定义 运算符 的具体含义,一个运算符重载函数的定义是 T operator 运算符 (参数) ,对于运算符重载有以 ...
- 【pytest】概述pytest——setup、teardown方法和conftest中的fixture用法,来执行测试用例的前置/后置条件语句操作
大家好,我是好学的小师弟.今天和大家分享下-在pytest中的前置/后置操作. 所谓的前置/后置操作,就是在测试用例执行前,你预先要执行的一些步骤:在测试用例执行完毕后,你要执行的一些数据清理/备份工 ...
- C++类与对象笔记十二:运算符重载二:左移运算符重载
左移运算符重载:可以打印输出自定义数据类型. 为了输出重载,我们先看看现有的输出函数.输出类型为std下的ostream类型的引用. 标准输出流(全局只能有一个). 返回值类型为ostream,函数名 ...
- 一元运算符重载 前置和后置++ --(这种一般用成员函数来实现重载)
#include <iostream>using namespace std; //实现一元运算符的前置重载 class A { public:friend A operator++(A ...
- C语言中的前置/后置++、- -
C语言中的++前置.后置++.- -前置.后置- - 在C语言中经常使用 ++ - - 单目操作符,那么前置和后置有什么区别呢? int main() {//前置:先操作,再使用//后置:先使用,再操 ...
- 运算符重载、STL模板库、类型转换、异常处理机制(C++五)
目录 一.运算符重载 1. 引入运算符重载 2.语法格式 3.运算符重载有两种表现形式 4.运算符重载分类 5.运算符重载的特点: 二.C++中的STL模板库的使用 1.容器类(container) ...
- 运算符重载——递增运算符重载
目录: 运算符重载--算术运算符重载 运算符重载--递增运算符重载 运算符重载--左移运算符重载 运算符重载--赋值运算符重载 应用场景: class MyInteger { public:MyInt ...
- c/c++入门教程 - 2.4.5 c++运算符重载(加号、左移、递增、赋值、关系、函数调用)
目录 4.5 运算符重载 4.5.1 加号运算符重载 4.5.2 左移运算符重载 4.5.3 递增运算符重载 4.5.4 赋值运算符重载 4.5.5 关系运算符重载 4.5.6 函数调用运算符重载 相 ...
- C++中的运算符重载基础
1.C++中的运算符重载基础 所谓重载,就是赋予新的含义.函数重载(Function Overloading)可以让一个函数名有多种功能,在不同情况下进行不同的操作.运算符重载(Operator Ov ...
最新文章
- mac mysql的安装
- JS-排序详解:冒泡排序、选择排序和快速排序
- linux SMbus错误
- 如何用 nodejs 编写TCP长连接应用
- mysql内存不断被占用,导致每隔一个多月就自动重启,修改数据库配置后,问题解决...
- Security:CSRF
- Cannot find source code based button in SE24
- 关于公司内部DNS的整改建议
- python constructor_Python学习札记(2)——搭建Boa-constructor
- 程序员面试 IT 公司,这些地方你要注意!
- LLDB使用详解以及断点调试教程
- preg_match_all() 函数
- java相关面试题汇总
- Android Material Component:工具栏与DisplayCutout
- 【干货】SSM,Jquery, Bootstrap从零打造一个论坛系统
- 四川汶川地震 各地伤亡汇总(实时更新)
- mysql中questions与com_select的区别
- h5和mysql做图书系统_JSP+Servlet+C3P0+Mysql实现的图书馆管理系统
- 百度手机输入法自定义码表
- 坚持到底就是成功,坚持到底就是富有,坚持到底就是胜利
热门文章
- python (六)函数
- python的字符串内建函数
- JDBC连接(MySql)数据库步骤,以及查询、插入、删除、更新等十一个处理数据库信息的功能。...
- C++:cin.getline
- 鱼油账号记录程序(续) - 零基础入门学习Delphi39
- 为何苦命干活的人成不了专家?
- java.util.hashmap_java.util.HashMap中的无限循环
- wps表格粗线和细线区别_详解论文中的表格技术
- linux 切换root_Linux运维服务篇:流量监控工具iftop部署及详细参数分享
- excel和python建模_利用Excel学习Python:准备篇