C++运算符重载详细解说及代码编写

2024-05-01 04:38

本文主要是介绍C++运算符重载详细解说及代码编写,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、不能重载的运算符:
  (1) "."(类成员访问运算符)
  (2)" .*"(类成员指针访问运算符)
  (3) "::"(域运算符)
  (4)"sizeof"(长度运算符)
  (5) " ?:"(条件运算符)
二、运算符
1.算术运算符    +   -   *   /    %
  2.关系运算符    >   <   >=  <= 
  3.逻辑运算符    &&  ||  !
  4.自增 自减    (前++  后++) (前--  后--)
  5.位运算符      &   |  
  6.赋值运算符    += -=   ==
  7.输入输出运算符 >>  <<
  7.其他运算符    ()  []  *   &   ->  取负-。。。
三、运算符重载:
  1.不能定义新的运算符,只能重载已有的运算符
  2.重载之后的运算符的优先级和结合性都不改变
  3.不能改变原运算符所需操作符的个数,同时至少要有一个操作数是自定义类型的操作数
  4.运算符重载后原语义没消失,只相当于针对特定的类定义了一个新的运算符
四、友元运算符重载和成员运算符重载的主要区别:
  1.参数个数不同
  2.友元函数没有this指针
  3.当运算符的左操作数是一个常数时,就不能利用this指针,应当用友元函数重载,例子见重载减运算符“+”
若运算符是一元的,则参数表为空,此时当前对象作为此运算符的单操作数;
  若运算符是二元的,则参数表中有一个操作数,此时当前对象作为此运算符的左操作数,参数表中的操作数作为此运算符的右操作数,以此类推。
五、源代码示例
#include<iostream>
using namespace std;
struct st
{int x;
};
st s;
class A
{int x;int arr[10];
public:A() { x = 0; cout << "调用无参构造" << endl; }A(int x) :x(x) { cout << "调用有参构造" << endl; }A(const A& other) :x(other.x) { cout << "调用拷贝构造" << endl; }~A() { cout << "调用析构函数" << endl; }//重载算术运算符  A operator+(const A& other);A operator-(const A& other);A operator*(const A& other);A operator/(const A& other);A operator%(const A& other);//重载关系运算符friend bool operator>(const A& a, const A& b);friend bool operator<(const A& a, const A& b);friend bool operator>=(const A& a, const A& b);friend bool operator<=(const A& a, const A& b);//自增自减A& operator++();//前++A operator++(int);//后++ 参数int不需要传参 与前++区分开A& operator--();//前--A operator--(int);//后--//赋值运算符A& operator+=(const A& other);A& operator-=(const A& other);bool operator==(const A& other);//输入输出运算符friend istream& operator >> (istream& is, A& other);//不可以用const A& 否则报错 因为other应为可修改的变量 const常量会导致other不可修改friend ostream& operator << (ostream& os, const A& other);//其他运算符() [] * &  ->  取负 -void operator()(int x, int y);int& operator[](size_t index);int& operator*();int* operator&();st* operator->();A operator-();};
//重载算术运算符 
//+运算符重载
A A::operator+(const A& other)
{return A(this->x+other.x);
}
//-运算符重载
A A::operator-(const A&other)//类外定义成员函数 函数名前面加上类名::
{return A(this->x - other.x);
}//*运算符重载
A A::operator*(const A&other)
{return A(this->x*other.x);
}//  /运算符重载
A A::operator/(const A&other)
{return A(this->x / other.x);
}//%运算符重载
A A::operator%(const A&other)
{return A(this->x%other.x);
}
//重载关系运算符
//>运算符重载
bool operator>(const A&a, const A&b)
{return a.x > b.x;
}//<运算符重载
bool operator<(const A&a, const A&b)
{return a.x < b.x;
}//>=运算符重载
bool operator>=(const A&a, const A&b)
{return a.x >= b.x;
}//<=运算符重载
bool operator<=(const A&a, const A&b)
{return a.x <= b.x;
}
//自增自减
//前++
A& A::operator++()
{++this->x;return *this;//返回值是引用,即返回的是对象本身,而不是临时对象
}
//返回引用和不返回引用 区别在于是否需要产生临时对象 ++++++a时,返回不是引用会不能连续前++ 如++++++a结果依旧为 1
//返回引用保证了地址在上一次前++处 也就意味着是在前一次++的基础上再++  如++++++a结果为 3
//参数int不需要传参  int用于区分前后++
//后++
A A::operator++(int)
{return A(this->x++);//不能返回对象本身 应返回临时对象(返回类型不是引用) 调用完后被析构先读取到this->x  表现出延迟性
}
//前--
A& A::operator--()
{--this->x;return *this;
}
//后--
A A::operator--(int)
{return A(this->x--);
}
//赋值运算符
//+=运算符
A& A::operator+=(const A& other)
{this->x += other.x;return *this;
}
//-=运算符
A& A::operator-=(const A& other)
{this->x -= other.x;return *this;
}
//==运算符
bool A::operator==(const A& other)
{return this->x == other.x;
}
//重载输入输出
//重载>>
istream& operator >> (istream& is, A& other)
{is >> other.x;return is;
}
//重载<<
ostream& operator << (ostream& os, const A& other)
{os << other.x;return os;
}//其他运算符    ()  []  *   &   ->  取负-
//()
void A::operator()(int x, int y)
{cout << "假装自己是函数名" << endl;cout << x << '\t' << y << endl;
}//[]
int& A::operator[](size_t index)
{return arr[index];
}
//*
int& A::operator*()
{return arr[0];//*arr 数组的地址
}
//&
int* A::operator&()//重载& 取地址  要求返回地址  这个地址是什么地址都可以
{return arr;
}
//->
st* A::operator->()//一般用于结构体指针 对象指针
{return &s;//返回结构体指针
}
A A::operator-()
{A a(-this->x);return a;
}
int main()
{A a, b,c;++++++a;cout << "++++++a=" << a << endl;b=a++;cout << "b=" << b <<'\t'<< "a=" << a << endl;c = a + b;cout << "c=a+b=" << c << endl;c = a - b;cout << "c=a-b=" << c << endl;c = a*b;cout << "c=a*b=" << c << endl;c = a / b;cout << "c=a/b=" << c << endl;c = a % b;cout << "c=a%b=" << c << endl;a += b;cout << "a+=b:" << a << endl;a -= b;cout << "a-=b:" << a << endl;if (a == b) { cout << "a==b"<<endl; }else { cout << "a!=b" << endl; }a(3,4);a[2] = 2;cout << "a[2]=" << a[2] << endl;a[0] = 1;*a = a[0];cout << "*a=" << *a << endl;cout << "&a=" << &a << endl;a->x = 8;cout << "a->x=" << a->x << endl;cin.get();return 0;
}
以上便是C++运算符重载的内容,希望对你有所帮助,欢迎在下方评价交流

这篇关于C++运算符重载详细解说及代码编写的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/950737

相关文章

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实