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++Stack栈类模版实例详解

    C++Stack栈类模版实例详解

    这篇文章主要为大家详细介绍了C++Stack栈类模版实例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • 关于函数调用方式__stdcall和__cdecl详解

    关于函数调用方式__stdcall和__cdecl详解

    下面小编就为大家带来一篇关于函数调用方式__stdcall和__cdecl详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • C语言 运算符详细介绍及示例代码

    C语言 运算符详细介绍及示例代码

    本文介绍C语言 运算符,这里整理了运算符的基础知识,并附示例代码,希望能帮助刚刚开始学习 C语言的同学
    2016-08-08
  • 如何判断一个整数的二进制中有多少个1

    如何判断一个整数的二进制中有多少个1

    本篇文章是对如何判断一个整数的二进制中有多少个1的方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • 浅析C/C++中被人误解的SIZEOF

    浅析C/C++中被人误解的SIZEOF

    以下是对C/C++中的SIZEOF进行了详细的分析介绍,需要的朋友参考下
    2013-07-07
  • C++线程池实现代码

    C++线程池实现代码

    C++11中,线程我们可以理解为对应一个thread对象,任务可以理解为要执行的函数,通常是耗时的函数。线程过多或者频繁创建和销毁线程会带来调度开销,进而影响缓存局部性和整体性能
    2021-12-12
  • C++标准库bitset类型的简单使用方法介绍

    C++标准库bitset类型的简单使用方法介绍

    这篇文章主要介绍了C++标准库bitset类型的简单使用方法,需要的朋友可以参考下
    2017-07-07
  • Qt重写QTreeView自绘实现酷炫样式

    Qt重写QTreeView自绘实现酷炫样式

    QTreeView,顾名思义,就是一种树形的控件,在我们需要做类似于文件列表的视图时,是一个不错的选择,下面我们就来看看qt如何重写QTreeView实现酷炫样式,感兴趣的可以了解一下
    2023-08-08
  • Visual Studio安装的图文教程

    Visual Studio安装的图文教程

    这篇文章主要介绍了Visual Studio安装的图文教程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • C++使用BitBlt进行窗口抓图的方法

    C++使用BitBlt进行窗口抓图的方法

    这篇文章主要介绍了C++使用BitBlt进行窗口抓图的方法,帮助大家更好的理解和使用c++,感兴趣的朋友可以了解下
    2021-01-01

最新评论