C/C++ 关于运算符重载笔记
加号运算符重载: 重载加号运算符,让 p3 = p1 + p2 改成 p3.mage = p1.mage + p2.mage 实现两个数据成员的相加。
告诉编译器,两个类中的数据成员应该怎么相加。
成员函数相加 +号运算符重载 成员函数 二元
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
int m_x;
int m_y;
Person(){};
Person(int x, int y) :m_x(x), m_y(y) {}
// 加号运算符重载,这里其实是二元,因为隐藏了一个this指针。
Person operator + (Person &p)
{
Person tmp;
tmp.m_x = this->m_x + p.m_x;
tmp.m_y = this->m_y + p.m_y;
return tmp;
}
};
int main(int argc, char *argv[])
{
Person p1(10, 10);
Person p2(20, 20);
Person p3 = p1 + p2;
cout << "p3 m_x = > " << p3.m_x << endl;
cout << "p3 m_y = > " << p3.m_y << endl;
system("pause");
return 0;
}
全局函数相加,实现运算符重载
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
int m_x;
int m_y;
Person(){};
Person(int x, int y) :m_x(x), m_y(y) {}
};
// 全局函数实现运算符重载,这个就属于二元运算符重载
Person operator +(Person &p1, Person &p2)
{
Person tmp;
tmp.m_x = p1.m_x + p2.m_x;
tmp.m_y = p1.m_y + p2.m_y;
return tmp;
}
int main(int argc, char *argv[])
{
Person p1(10, 30);
Person p2(20, 50);
Person p3 = p1 + p2;
cout << "p3 m_x = > " << p3.m_x << endl;
cout << "p3 m_y = > " << p3.m_y << endl;
system("pause");
return 0;
}
左移运算符重载: 使用 << 重载左移运算符,让cout 直接输出两个变量。重载左移运算符不可以写成成员函数,只能写全局运算符。
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
int m_x;
int m_y;
Person(){};
Person(int x, int y) :m_x(x), m_y(y) {}
};
ostream& operator << (ostream &cout, Person &ptr)
{
cout << "m_x = " << ptr.m_x << " ----> " << "m_y = " << ptr.m_y << endl;
return cout;
}
int main(int argc, char *argv[])
{
Person p1(10, 30);
Person p2(20, 10);
cout << p1 << endl;
cout << p2 << endl;
system("pause");
return 0;
}
使用友元函数,访问私有的数据。
#include <iostream>
#include <string>
using namespace std;
class Person
{
friend ostream& operator<<(ostream &cout, Person &ptr);
private:
int m_x;
int m_y;
public:
Person(){};
Person(int x, int y) :m_x(x), m_y(y) {}
};
ostream& operator << (ostream &cout, Person &ptr)
{
cout << "m_x = " << ptr.m_x << " ----> " << "m_y = " << ptr.m_y << endl;
return cout;
}
int main(int argc, char *argv[])
{
Person p1(10, 30);
Person p2(20, 10);
cout << p1 << endl;
cout << p2 << endl;
system("pause");
return 0;
}
前置/后置运算符的重载:
#include <iostream>
#include <string>
using namespace std;
class MyInteger
{
friend ostream& operator<<(ostream& cout, MyInteger & myInt);
public:
int m_count;
public:
MyInteger() { m_count = 0; }
// 重载前置 ++x 运算符
MyInteger& operator ++ ()
{
this->m_count++;
return *this;
}
// 重载后置 x++ 运算符,为了区分前后置,需要在参数后面增加一个int占位符
// 此时编译器才会认为我们需要使用后置重载运算符了
MyInteger operator ++ (int)
{
MyInteger tmp = *this;
m_count++;
return tmp;
}
};
ostream& operator<<(ostream& cout, MyInteger & myInt)
{
cout << myInt.m_count;
return cout;
}
int main(int argc, char *argv[])
{
MyInteger myInt;
cout << ++myInt << endl;
cout << myInt++ << endl;
cout << ++(++myInt) << endl;
system("pause");
return 0;
}
指针运算符重载(智能指针) 用来托管自定义的对象,让对象可以自动的释放数据,
当我们使用一个对象结束以后,无需手动释放堆空间,智能指针会帮助我们完成这个过程。
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
char *m_name;
int m_age;
public:
Student(char *name, int age)
{
this->m_name = name;
this->m_age = age;
}
void Print()
{
cout << "Name: " << this->m_name << endl;
cout << "Age: " << this->m_age << endl;
}
};
// 定义智能指针,用于自动释放对象所占用的空间
class Smart_Pointer
{
private:
Student *ptr;
public:
// 先来执行构造函数,将传入的指针复制到内部
Smart_Pointer(Student *ptr)
{ this->ptr = ptr; }
// 重载运算符 -> 让智能指针能够直接指向Student
Student * operator -> ()
{ return this->ptr; }
// 重载运算符 *
Student & operator * ()
{ return *this->ptr; }
// 定义析构函数,这是智能指针的关键部分,对象会被自动释放
~Smart_Pointer()
{
if (this->ptr != NULL)
{
delete this->ptr;
this->ptr = NULL;
}
}
};
int main(int argc, char *argv[])
{
// 手动释放的案例:平常的使用方式
Student *stu = new Student("lyshark", 10);
stu->Print();
delete stu;
// 使用智能指针:则无需考虑释放的问题
Smart_Pointer ptr(new Student("lyshark", 10));
ptr->Print();
(*ptr).Print();
system("pause");
return 0;
}
赋值运算符重载: 我们将等于号进行重载,实现对类中数据成员的赋值拷贝。
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
int m_uid;
char *m_name;
public:
Student(int uid, char *name)
{
this->m_uid = uid;
this->m_name = new char[strlen(name) + 1];
strcpy(this->m_name, name);
}
// 重载 = 实现类数据成员的赋值运算
Student& operator = (const Student &ptr)
{
// 先来判断原来的堆区是否有内容,如果有则先来释放
if (this->m_name != NULL)
{
this->m_uid = 0;
delete[] this->m_name;
this->m_name = NULL;
}
// 否则,我们直接开辟空间完成内存拷贝
this->m_name = new char[strlen(ptr.m_name) + 1];
strcpy(this->m_name, ptr.m_name);
this->m_uid = ptr.m_uid;
return *this;
}
// 析构函数,则需要释放内存
~Student()
{
if (this->m_name != NULL)
{
this->m_uid = 0;
delete[] this->m_name;
this->m_name = NULL;
}
}
};
int main(int argc, char *argv[])
{
Student stu1(1,"lyshark");
Student stu2(2, "admin");
Student stu3(0, "");
stu3 = stu2 = stu1;
cout << stu3.m_name << endl;
cout << stu2.m_name << endl;
cout << stu1.m_name << endl;
system("pause");
return 0;
}
关系运算符重载:
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
int m_uid;
char * m_name;
public:
Student(int uid,char *name)
{
this->m_uid = uid;
this->m_name = name;
}
bool operator == (Student &ptr)
{
if (this->m_uid == ptr.m_uid && this->m_name == ptr.m_name)
return true;
return false;
}
bool operator != (Student &ptr)
{
if (this->m_uid != ptr.m_uid && this->m_name != ptr.m_name)
return true;
return false;
}
};
int main(int argc, char *argv[])
{
Student stu1(1, "lyshark");
Student stu2(1, "lyshark");
Student stu3(2, "admin");
if (stu1 == stu2)
cout << "stu1 = stu2" << endl;
if (stu1 != stu3)
cout << "stu1 != stu3" << endl;
system("pause");
return 0;
}
重载与仿函数:
#include <iostream>
#include <string>
using namespace std;
class MyPrint
{
public: void operator()(string text)
{
cout << text << endl;
}
};
class MyAdd
{
public: int operator()(int x, int y)
{
return x + y;
}
};
int main(int argc, char *argv[])
{
MyPrint print;
print("hello lyshark"); // 使用仿函数
cout << MyAdd()(100, 200) << endl; // 匿名仿函数
system("pause");
return 0;
}
C/C++ 关于运算符重载笔记的更多相关文章
- c++运算符重载笔记
运算符重载的概念:给原有的运算符赋予新的功能: 比如:+ 不仅可以做算术运算也可以连接俩个字符串 一元运算符:只与一个操作数进行运算 比如 正负号 运算符重载的本质是:函数重载. <<与& ...
- C++学习笔记之运算符重载
一.运算符重载基本知识 在前面的一篇博文 C++学习笔记之模板(1)——从函数重载到函数模板 中,介绍了函数重载的概念,定义及用法,函数重载(也被称之为函数多态)就是使用户能够定义多个名称相同但特征标 ...
- 初步C++运算符重载学习笔记<3> 增量递减运算符重载
初步C++运算符重载学习笔记<1> 初探C++运算符重载学习笔记<2> 重载为友元函数 增量.减量运算符++(--)分别有两种形式:前自增++i(自减--i).后自增i ...
- 初探C++运算符重载学习笔记<2> 重载为友元函数
初探C++运算符重载学习笔记 在上面那篇博客中,写了将运算符重载为普通函数或类的成员函数这两种情况. 以下的两种情况发生.则我们须要将运算符重载为类的友元函数 <1>成员函数不能满足要求 ...
- c++中的运算符重载operator2(翁恺c++公开课[31-33]学习笔记)
上一篇operator1中,大概说了下重载的基本用法,接下来对c++中常见的可重载运算符归一下类,说一下它们的返回值,讨论下较为复杂的运算符重载上的坑
- c++中的运算符重载operator1(翁恺c++公开课[30]学习笔记)
运算符重载规则: 只有已经存在的运算符才能被重载,不能自己制造一个c++中没有的运算符进行重载 重载可以在类或枚举类型内进行,也可以是全局函数,但int.float这种已有的类型内是不被允许的 不能二 ...
- C++基础 学习笔记五:重载之运算符重载
C++基础 学习笔记五:重载之运算符重载 什么是运算符重载 用同一个运算符完成不同的功能即同一个运算符可以有不同的功能的方法叫做运算符重载.运算符重载是静态多态性的体现. 运算符重载的规则 重载公式 ...
- C#高级编程笔记2016年10月12日 运算符重载
1.运算符重载:运算符重重载的关键是在对象上不能总是只调用方法或属性,有时还需要做一些其他工作,例如,对数值进行相加.相乘或逻辑操作等.例如,语句if(a==b).对于类,这个语句在默认状态下会比较引 ...
- C++ Primer笔记10_运算符重载_赋值运算符_进入/输出操作符
1.颂值运营商 首先来福值运算符引入后面要说的运算符重载.上一节说了构造函数.拷贝构造函数:一个类要想进行更好的控制.须要定义自己的构造函数.拷贝构造函数.析构函数.当然,还有赋值运算符.常说的三大函 ...
- 《Inside C#》笔记(十一) 运算符重载
运算符重载与之前的索引器类似,目的是为了让语言本身使用起来更方便直接,也是一种语法糖. 一 运算符重载(Operator Overloading) 运算符重载的存在,使得现有的各种运算符可以被重新定义 ...
随机推荐
- # github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
linux使用go连接etcd集群时报错: # github.com/coreos/etcd/clientv3/balancer/resolver/endpoint /root/go/pkg/mod/ ...
- protobuf安装、编译和使用
protobuf使用简单示例 一.安装 首先下载protobuf的安装包,我这里使用的是protobuf-cpp-3.21.5.tar.gz 解压安装包 tar -xzf protobuf-cpp-3 ...
- Codeforce 1335C - Two Teams Composing 统计技能种类数量+统计同一技能最大数量
7 4 2 4 1 4 3 4 统计技能种类数量 4种不同技能 统计同一技能最大数量 技能1(数量1) 技能2(数量1) 技能3(数量1) 技能4(数量4) 选出 技能4(数量4) 作为 第2组 扣除 ...
- 【每日一题】8.Shortest Path (树上DFS)
题目链接:Here 题意总结:给定的是无向图(树),要求把分成 \(n/2\) 对 让权值最小 思路: 看一下范围 在加上是一棵树 所以做法应该是dfs 复杂度为 \(\mathcal{O}(n)\) ...
- 如何让 Llama2、通义千问开源大语言模型快速跑在函数计算上?
本文是"在Serverless平台上构建AIGC应用"系列文章的第一篇文章. 前言 随着ChatGPT 以及 Stable Diffusion,Midjourney 这些新生代 A ...
- 【驱动】SPI驱动分析(六)-RK SPI驱动分析
前言 Linux的spi接口驱动实现目录在kernel\drivers\spi下.这个目录和一些层次比较明显的驱动目录布局不同,全放在这个文件夹下,因此还是只好通过看Kconfig 和 Makefil ...
- connect() failed (113: No route to host) while connecting to upstream
转载请注明出处: 用docker 搭建的服务,今天重启了一个容器之后,请求这个服务的接口都变成了 502, 但通过docker 查看日志,发现没有异常,端口也都是正常,在nginx的日志中看到了这段错 ...
- [转帖]TLS/SSL (Schannel SSP) 中的密码套件
https://learn.microsoft.com/zh-cn/windows/win32/secauthn/cipher-suites-in-schannel 密码套件是一组加密算法. TLS/ ...
- [转帖]一文说清 Linux System Load
https://zhuanlan.zhihu.com/p/447661302 双十一压测过程中,常见的问题之一就是load 飙高,通常这个时候业务上都有受影响,比如服务rt飙高,比如机器无法登录,比如 ...
- [转帖]Flink完全分布式集群安装
https://zhuanlan.zhihu.com/p/131592261 Flink支持完全分布式模式,这时它由一个master节点和多个worker节点构成.在本节,我们将搭建一个如下的三个节点 ...