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

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. Android Studio 之 GridView

    1. 数据源 ArrayList Cursor 2. 适配器 Adapter ArrayAdapter SmipleAdapter SmipleCursorAdapter BaseAdapter

  2. Django学习----js传参给view.py

    需求: 散点图中每选择一个点,获取到id之后传给view.py,根据这个id进行sql语句的查询. 问题: 要求实时查询 解决办法: ajax查询 js页面 .on("mousedown&q ...

  3. Python(三)对装饰器的理解

    装饰器是 Python 的一个重要部分,也是比较难理解和使用好的部分.下面对装饰器做一下简单整理 1. 前言 装饰器实际上是应用了设计模式里,装饰器模式的思想: 在不概念原有结构的情况下,添加新的功能 ...

  4. volatile 和 内存屏障

    接下来看看volatile是如何解决上面两个问题的: 被volatile修饰的变量在编译成字节码文件时会多个lock指令,该指令在执行过程中会生成相应的内存屏障,以此来解决可见性跟重排序的问题. 内存 ...

  5. HashTable源码

    1. 为什么无法创建更大的数组? Attempts to allocate larger arrays may result in OutOfMemoryError 如果数组长度过大,可能出现的两种错 ...

  6. sql优化(原理,方法,特点,实例)

    整理的有点多,做好心理准备...... 1.资源优化理解: 不同设备,io不同.每种设备都有两个指标:延时(响应时间):表示硬件的突发处理能力:带宽(吞吐量):代表硬件持续处理能力. 每种硬件主要的工 ...

  7. 认清楚服务器的真正身份--深入ARP工作原理

    我们知道IP地址是ISP分配给我们的,IP不能作为服务器的唯一的身份,那么服务器真正的身份是什么呢?MAC IP地址直接的通信在底层要转换到MAC直接的通信,那他们如何通信的呢? 1.简介 出场人物: ...

  8. NETCore使用带有权限验证的Swagger

    原文:NETCore使用带有权限验证的Swagger 文章目录 Swagger 什么是Swagger NuGet安装 Startup注册Swagger 设置默认首页打开Swagger 为接口添加注释 ...

  9. axios捕获401 赋值token

    //捕获401 // http request 拦截器 axios.interceptors.request.use( config => { const token = localStorag ...

  10. 千万不要用window自带文本编辑器编辑配置文件或者代码

    1 引言 用windows自带的text文本在最前面会带入看不到的BOM,导致异常 2 代码 package main import ( "strings" "fmt&q ...