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

相关文章

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

2025版mysql8.0.41 winx64 手动安装详细教程

《2025版mysql8.0.41winx64手动安装详细教程》本文指导Windows系统下MySQL安装配置,包含解压、设置环境变量、my.ini配置、初始化密码获取、服务安装与手动启动等步骤,... 目录一、下载安装包二、配置环境变量三、安装配置四、启动 mysql 服务,修改密码一、下载安装包安装地

在macOS上安装jenv管理JDK版本的详细步骤

《在macOS上安装jenv管理JDK版本的详细步骤》jEnv是一个命令行工具,正如它的官网所宣称的那样,它是来让你忘记怎么配置JAVA_HOME环境变量的神队友,:本文主要介绍在macOS上安装... 目录前言安装 jenv添加 JDK 版本到 jenv切换 JDK 版本总结前言China编程在开发 Java

Spring Boot Actuator应用监控与管理的详细步骤

《SpringBootActuator应用监控与管理的详细步骤》SpringBootActuator是SpringBoot的监控工具,提供健康检查、性能指标、日志管理等核心功能,支持自定义和扩展端... 目录一、 Spring Boot Actuator 概述二、 集成 Spring Boot Actuat

C++中detach的作用、使用场景及注意事项

《C++中detach的作用、使用场景及注意事项》关于C++中的detach,它主要涉及多线程编程中的线程管理,理解detach的作用、使用场景以及注意事项,对于写出高效、安全的多线程程序至关重要,下... 目录一、什么是join()?它的作用是什么?类比一下:二、join()的作用总结三、join()怎么

如何在Java Spring实现异步执行(详细篇)

《如何在JavaSpring实现异步执行(详细篇)》Spring框架通过@Async、Executor等实现异步执行,提升系统性能与响应速度,支持自定义线程池管理并发,本文给大家介绍如何在Sprin... 目录前言1. 使用 @Async 实现异步执行1.1 启用异步执行支持1.2 创建异步方法1.3 调用

Spring Boot 与微服务入门实战详细总结

《SpringBoot与微服务入门实战详细总结》本文讲解SpringBoot框架的核心特性如快速构建、自动配置、零XML与微服务架构的定义、演进及优缺点,涵盖开发环境准备和HelloWorld实战... 目录一、Spring Boot 核心概述二、微服务架构详解1. 微服务的定义与演进2. 微服务的优缺点三