当前位置: 首页 > news >正文

C++之运算符重载详解篇

1.概念        

重载概念:

C++ 允许在同一作用域中的某个函数运算符指定多个定义,分别称为函数重载运算符重载

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

这里主要介绍一下运算符重载。

重载大部分 C++ 内置的运算符。这样,您就能使用自定义类型的运算符

重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表

        大多数的重载运算符可被定义为普通的非成员函数或者被定义为类成员函数。如果我们定义上面的函数为类的非成员函数,那么我们需要为每次操作传递两个参数,如下所示:

Person operator+(const Person& p1,const Person& p2)

        如果是成员函数,对象作为参数进行传递,对象的属性使用 this 运算符进行访问

//成员函数实现 + 号运算符重载
Person operator+(const Person& p) {
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}

2.加号运算符重载

作用:

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

代码

class Person {
public:
Person() {};
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
//成员函数实现 + 号运算符重载
Person operator+(const Person& p) {
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
public:
int m_A;
int m_B;
};
//全局函数实现 + 号运算符重载
//Person operator+(const Person& p1, const Person& p2) {
// Person temp(0, 0);
// temp.m_A = p1.m_A + p2.m_A;
// temp.m_B = p1.m_B + p2.m_B;
// return temp;
//}
//运算符重载 可以发生函数重载
Person operator+(const Person& p2, int val)
{
Person temp;
temp.m_A = p2.m_A + val;
temp.m_B = p2.m_B + val;
return temp;
}
void test() {
Person p1(10, 10);
Person p2(20, 20);
//成员函数方式
Person p3 = p2 + p1; //相当于 p2.operaor+(p1)
cout << "mA:" << p3.m_A << " mB:" << p3.m_B << endl;
Person p4 = p3 + 10; //相当于 operator+(p3,10)
cout << "mA:" << p4.m_A << " mB:" << p4.m_B << endl;
}
int main() {
test();
system("pause");
return 0;
}

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

3.左移运算符重载

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

class Person {
friend ostream& operator<<(ostream& out, Person& p);
public:
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
//成员函数 实现不了 p << cout 不是我们想要的效果
//void operator<<(Person& p){
//}
private:
int m_A;
int m_B;
};
//全局函数实现左移重载
//ostream对象只能有一个
ostream& operator<<(ostream& out, Person& p) {
out << "a:" << p.m_A << " b:" << p.m_B;
return out;
}
void test() {
Person p1(10, 20);
cout << p1 << "hello world" << endl; //链式编程
}
int main() {
test();
system("pause");
return 0;
}

总结:重载左移运算符配合友元可以实现输出自定义数据类型

4.递增运算符重载

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

class MyInteger {
friend ostream& operator<<(ostream& out, MyInteger myint);
public:
MyInteger() {
m_Num = 0;
}
//前置++
MyInteger& operator++() {
//先++
m_Num++;
//再返回
return *this;
}
//后置++
MyInteger operator++(int) {
//先返回
MyInteger temp = *this; //记录当前本身的值,然后让本身的值加1,但是返回的是以前的
值,达到先返回后++;
m_Num++;
return temp;
}
private:
int m_Num;
};
ostream& operator<<(ostream& out, MyInteger myint) {
out << myint.m_Num;
return out;
}
//前置++ 先++ 再返回
void test01() {
MyInteger myInt;
cout << ++myInt << endl;
cout << myInt << endl;
}
//后置++ 先返回 再++
void test02() {
MyInteger myInt;
cout << myInt++ << endl;
cout << myInt << endl;
}
int main() {
test01();
//test02();
system("pause");return 0;
}

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

5.赋值运算符重载

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

1. 默认构造函数(无参,函数体为空)
2. 默认析构函数(无参,函数体为空)
3. 默认拷贝构造函数,对属性进行值拷贝
4. 赋值运算符 operator=, 对属性进行值拷贝
如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

避免浅拷贝问题:

那如果类中有属性需要开辟堆空间,如果避免浅拷贝问题呢?
1. 需要定义拷贝构造函数和赋值函数
2. 或者,就禁用这两个函数(用private)

class Person
{
public:
Person(int age)
{
//将年龄数据开辟到堆区
m_Age = new int(age);
}
//重载赋值运算符
Person& operator=(Person &p)
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
//编译器提供的代码是浅拷贝
//m_Age = p.m_Age;
//提供深拷贝 解决浅拷贝的问题
m_Age = new int(*p.m_Age);
//返回自身
return *this;
}
~Person()
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
}
//年龄的指针
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();
//int a = 10;
//int b = 20;
//int c = 30;
//c = b = a;
//cout << "a = " << a << endl;
//cout << "b = " << b << endl;
//cout << "c = " << c << endl;
system("pause");
return 0;
}

6.关系运算符重载

作用:

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

代码

#include <iostream>
#include <string>
using namespace std;
class Person
{public:Person(const string& name, int age){this->m_Name = name;this->m_Age = age;};bool operator==(const Person& p){return (this->m_Name == p.m_Name && this->m_Age == p.m_Age);}bool operator!=(const Person & p){return !(this->m_Name == p.m_Name && this->m_Age == p.m_Age);}private:string m_Name;int m_Age;
};
void test01()
{Person a("孙悟空", 18);Person b("孙悟空", 18);if (a == b){cout << "a和b相等" << endl;}else{cout << "a和b不相等" << endl;}if (a != b){cout << "a和b不相等" << endl;}else{cout << "a和b相等" << endl;}
}
int main() {test01();// system("pause");getchar();return 0;
}

7.函数调用运算符重载

  • 函数调用运算符 () 也可以重载
  • 由于重载后使用的方式非常像函数的调用,因此称为仿函数
  • 仿函数没有固定写法,非常灵活
class MyPrint
{public:void operator()(string text){cout << text << endl;}
};
void test01()
{//重载的()操作符 也称为仿函数MyPrint myFunc;myFunc("hello world");
}
class MyAdd
{public:int operator()(int v1, int v2){return v1 + v2;}
};
void test02()
{MyAdd add;int ret = add(10, 10);cout << "ret = " << ret << endl;//匿名对象调用cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}
int main() {test01();test02();system("pause");return 0;
}

http://www.lryc.cn/news/514592.html

相关文章:

  • 深度学习应用工程化中的节能减排最佳实践
  • 电脑文件msvcp110.d丢失的解决方法
  • xdoj isbn号码
  • qt的utc时间转本地时间
  • mariadb变更数据存放目录
  • 分布式专题(11)之Zookeeper特性与节点数据类型详解
  • Java项目实战II基于小程序的驾校管理系统(开发文档+数据库+源码)
  • Unity Pico 应用失去焦点后,追踪功能被禁用(原生 UI 界面弹出)
  • 第十四届蓝桥杯Scratch省赛中级组—智能计价器
  • AWS S3文件存储工具类
  • 【leetcode100】二叉树的中序遍历
  • 开源GTKSystem.Windows.Forms框架:C# Winform跨平台运行深度解析
  • C++软件设计模式之责任链模式
  • 021-spring-springmvc-组件
  • 基于SpringBoot和OAuth2,实现通过Github授权登录应用
  • macos 支持外接高分辩率显示器开源控制软件
  • C++26 新特性预览(Preview)
  • MySQL5.7.26-Linux-安装(2024.12)
  • 2025-1-2-sklearn学习(30)模型选择与评估-验证曲线: 绘制分数以评估模型 真珠帘卷玉楼空,天淡银河垂地。
  • 【优选算法】查找总价格为目标值的两个商品
  • 利用 NineData 实现 PostgreSQL 到 Kafka 的高效数据同步
  • future和CompletableFuture
  • 如何通过深度学习提升大分辨率图像预测准确率?
  • 【机器学习】机器学习的基本分类-半监督学习-Ladder Networks
  • [react]小技巧, ts如何声明点击事件的类型
  • 智能工厂的设计软件 应用场景的一个例子:为AI聊天工具添加一个知识系统 之9 重新开始 之2
  • 【从零开始】11. LLaMA-Factory 微调 Qwen 模型(番外篇)
  • WPF使用ContentControl控件实现区域导航,并使用Prism依赖注入优化
  • JavaWeb——MySQL-DML(1/3)-添加数据insert(DML 操作概述、INSERT 语句插入数据、语句演示、总结)
  • 经验证:将数据从索尼传输到Android的 4 种方法