运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

1.加号运算符重载

作用:实现两个自定义数据类型相加的运算

#include <iostream>
#include <string>
using namespace std; //加号运算符重载
class Person{
public:
//1.成员函数重载+号
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;
}
int m_a;
int m_b;
}; //2.全局函数重载+号
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;
} //3.函数重载的版本
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 = 10;
p1.m_b = 10;
Person p2;
p2.m_a = 10;
p2.m_b = 10; //成员函数重载的本质调用
//Person p3 = p1.operator+(p2); //全局函数重载的本质调用
//Person p3 = operator+(p1,p2);
Person p3 = p1 + p2; //运算符重载也可以发生函数重载
Person p4 = p3 + 100; cout << "p3.m_a = " << p3.m_a <<endl;
cout << "p3.m_b = " << p3.m_b <<endl;
cout << "p4.m_a = " << p4.m_a <<endl;
cout << "p4.m_b = " << p4.m_b <<endl;
} int main(){
test01();
return 0;
}

总结1:对于内置的数据类型的表达式的运算符是不可能改变的

总结2:不要滥用运算符重载

2.左移运算符重载

作用:可以自输出自定义数据类型

#include <iostream>
#include <string>
using namespace std; //左移运算符重载
class Person{
public:
//1.利用成员函数重载左移运算符 p.operator<<(cout) 简化版本p<<cout
//通常不会利用成员函数重载左移运算符,因为无法实现cout在左侧
int m_a;
int m_b;
}; //2.利用全局函数重载左移运算符
ostream & operator<<(ostream &cout,Person &p){
cout << "m_a = " << p.m_a << " m_b = " << p.m_b << endl;
return cout;
//必须返回引用,因为只能存在一个,不能开辟新的空间
} void test01(){
Person p;
p.m_a = 10;
p.m_b = 10; cout << "p.m_a = " << p.m_a <<endl;
cout << "p.m_b = " << p.m_b <<endl;
cout << p << endl;
} int main(){
test01();
return 0;
}
  • 通常不会利用成员函数重载左移运算符,因为无法实现cout在左侧

    利用成员函数重载左移运算符 p.operator<<(cout) 简化版本p<<cout

  • 左移运算符重载必须返回ostream引用,因为只能存在一个,不能开辟新的空间

3. 递增运算符重载

作用:通过重载递增运算符,实现自己的整型数据

注意:前置返回的是变量的引用,后置返回的是常量。所以++++c合法,而c++++不合法

#include <iostream>
#include <string>
using namespace std; //重载递增运算符 //自定义整型
class MyInterger{
friend ostream & operator<<(ostream &cout, MyInterger myint);
public:
MyInterger(){
m_num = 0;
}
//1.重载前置++运算符
//返回引用是为了一直对一个数据进行递增操作
MyInterger & operator++(){
m_num = m_num+1;
return *this; //返回自身
}
//2.重载后置++运算符
//int代表一个占位参数,可以用于区分前置和后置递增
//后置递增一定不能返回引用,因为temp为局部变量,函数运行完后自动释放
MyInterger operator++(int){
//先 记录当时结果
MyInterger temp = *this;
//后 递增
m_num = m_num+1;
//最后 返回所记录结果
return temp;
}
private:
int m_num;
}; //重载左移运算符
ostream & operator<<(ostream &cout, MyInterger myint){
cout << myint.m_num;
return cout;
} void test01(){
MyInterger myint;
cout << ++myint << endl;
} void test02(){
MyInterger myint;
cout << myint++ << endl;
cout << myint << endl;
} int main(){
test02();
return 0;
}

总结:前置递增返回的是引用,后置递增返回的是值

4.赋值运算符重载

c++编译器至少给一个类添加4个函数

  1. 默认构造函数(无参,函数体为空)
  2. 默认析构函数(无参,函数体为空)
  3. 默认拷贝构造函数,对属性值进行拷贝
  4. 赋值运算符operator=,对属性值进行拷贝

如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

#include<iostream>
using namespace std; class Person {
public:
Person(int age) {
m_age = new int(age);
}
~Person() {
if (m_age != NULL) {
delete m_age;
m_age = NULL;
}
} //重载赋值运算符
Person& operator=(Person &p) {
//编译器提供的浅拷贝
//m_age = p.m_age
//应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
if (m_age != NULL) {
delete m_age;
m_age = NULL;
}
m_age = new int(*p.m_age);
return *this;
}
int *m_age;
}; void test01() {
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1; //赋值操作
cout << "p1的年龄为:" << *p1.m_age << endl;
cout << "p2的年龄为:" << *p2.m_age << endl;
cout << "p3的年龄为:" << *p3.m_age << endl;
} int main() {
test01();
system("pause");
return 0;
}

5.关系运算符重载

作用:重载关系运算符,可以让两个自定义类型对象进行对比操作

#include<iostream>
#include<string>
using namespace std; class Person {
public:
Person(string name, int age) {
m_name = name;
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("xia",18);
Person p2("xia", 18); if (p1 == p2) {
cout << "p1 == p2" << endl;
}
else {
cout << "p1 != p2" << endl;
}
} int main() {
test01();
return 0;
}

6.函数调用运算符重载

  • 函数调用运算符()也可以重载
  • 由于重载后使用的方式非常像函数的调用,因此称为仿函数
  • 仿函数没有固定写法,非常灵活
#include<iostream>
#include<string>
using namespace std; //函数调用运算符的重载 //打印输出类
class MyPrint {
public:
//重载函数调用运算符
void operator()(string test) {
cout << test << endl;
}
}; class MyAdd {
public:
int operator()(int num1, int num2) {
return num1 + num2;
}
}; void MyPrint02(string test) {
cout << test << endl;
} void test01() {
MyPrint myPrint;
myPrint("hello world"); //由于使用起来非常类似于函数调用,因此称为仿函数
MyPrint02("hello world");
} void test02() {
MyAdd myadd;
int ret = myadd(100, 100);
cout << "ret = " << ret << endl;
//匿名函数对象
cout << MyAdd()(100, 100) << endl;
} int main() {
test01();
test02();
return 0;
}

C++学习(12)—— 运算符重载的更多相关文章

  1. C++学习之运算符重载的总结

    C++学习之运算符重载的总结              运算符重载是对已有的运算符赋予多重含义,使同一个运算符作用域不同类型的数据导致不同行为的发生,C++为运算符重载提供了一种方法,即运算符重载函数 ...

  2. C++学习笔记-运算符重载

    运算符重载使得用户自定义的数据以一种更简洁的方式工作 运算符重载规则 重载运算符的限制 可以重载的运算符 + - * / % ^ & | ~ ! = < > += -= *= /= ...

  3. 我的c++学习(8)运算符重载和友元

    运算符的重载,实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该运算符时就调用此函数来行使运算符功能.这个函数叫做运算符重载函数(常为类的成员函数). 方法与解释 ◆ 1.定义运 ...

  4. C++学习26 运算符重载的概念和语法

    所谓重载,就是赋予新的含义.函数重载(Function Overloading)可以让一个函数名有多种功能,在不同情况下进行不同的操作.运算符重载(Operator Overloading)也是一个道 ...

  5. STL学习之运算符(<<)重载问题和仿函数的实现

    /*   运算符<<的重载一直报错,   友原函数中可以访问到类的私有成员*/#include<iostream>using namespace std; class MyIn ...

  6. 运算符重载+日期类Date

    Hello,一只爱学习的鱼 大学学习C++运算符重载的时候,老师出了一道"运算符重载+类"的综合练习题,让我们来一起看看吧! 题目: 设计一个日期类Date,包括年.月.日等私有成 ...

  7. 初步C++运算符重载学习笔记&lt;3&gt; 增量递减运算符重载

    初步C++运算符重载学习笔记<1> 初探C++运算符重载学习笔记<2> 重载为友元函数     增量.减量运算符++(--)分别有两种形式:前自增++i(自减--i).后自增i ...

  8. C++运算符重载学习总结

    在C ++中,我们可以使运算符适用于用户定义的类. 这意味着C ++能够为运算符提供数据类型的特殊含义,这种能力称为运算符重载. 例如,我们可以在像String这样的类中重载运算符'+',这样我们就可 ...

  9. C#高级编程笔记2016年10月12日 运算符重载

    1.运算符重载:运算符重重载的关键是在对象上不能总是只调用方法或属性,有时还需要做一些其他工作,例如,对数值进行相加.相乘或逻辑操作等.例如,语句if(a==b).对于类,这个语句在默认状态下会比较引 ...

随机推荐

  1. 使用 AI 绘制箭头

    参考下面教程 有空再整理 https://jingyan.baidu.com/article/7082dc1cd77899e40b89bd5a.html http://www.360doc.com/c ...

  2. springboot修改页面不用重启的设置(idea)

       1) “File” -> “Settings” -> “Build,Execution,Deplyment” -> “Compiler”,选中打勾 “Build project ...

  3. 怎么删除iOS模拟器上的应用程序?

    怎么删除iOS模拟器上的应用程序: 和手机上一样,鼠标长按,点击删除 xcode 卸载模拟器 Simulator:删除目录/Library/Developer/CoreSimulator/Profil ...

  4. Arcmap图层浏览遇到ORA-07445 [QCDLAUCN] 错误

    Oracle 12.1.0.2版本,在图层浏览时遇到了ORA-07445 [QCDLAUCN] 错误.根据MOS的查询结果,得知这是一个bug (Doc ID 1932725.1): 文章中同时给出了 ...

  5. Linux内核引用计数器kref结构

    1.前言 struct kref结构体是一个引用计数器,它被嵌套进其它的结构体中,记录所嵌套结构的引用计数.引用计数用于检测内核中有多少地方使用了某个对象,每当内核的一个部分需要某个对象所包含的信息时 ...

  6. 适合 ASP.NET Core 的超级-DRY开发

    作者 Thomas Hansen DRY 是那些非常重要的软件体系结构缩写之一.它的意思是“不要自我重复”,并向维护旧源代码项目的任何用户阐明了一个重要原则.也就是说,如果你在代码中自我重复,会发现每 ...

  7. python实现栈结构

    # -*- coding:utf-8 -*- # __author__ :kusy # __content__:文件说明 # __date__:2018/9/30 17:28 class MyStac ...

  8. Linux shell脚本单例模式实现

    一.说明 关于单例模式,最开始的是一些小工具,运行起来后再点击运行时会提示已经运行了一个实例,觉得挺有意思但也没有很在意. 前段时间看了前领导的一段代码不太懂是做什么用的,同事查了下资料说是为了实现单 ...

  9. 页面嵌入iframe关于父子传参调用

    1.首先来说一下iframe是干什么用的 IFRAME是HTML标签,作用是文档中的文档,或者浮动的框架(FRAME).iframe元素会创建包含另外一个文档的内联框架(即行内框架),通俗点说就是在一 ...

  10. CLH lock 原理及JAVA实现

    --喜欢记得关注我哟[shoshana]--​ 前记 JUC中的Lock中最核心的类AQS,其中AQS使用到了CLH队列的变种,故来研究一下CLH队列的原理及JAVA实现 一. CLH背景知识 SMP ...