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

相关文章

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

Goland debug失效详细解决步骤(合集)

《Golanddebug失效详细解决步骤(合集)》今天用Goland开发时,打断点,以debug方式运行,发现程序并没有断住,程序跳过了断点,直接运行结束,网上搜寻了大量文章,最后得以解决,特此在这... 目录Bug:Goland debug失效详细解决步骤【合集】情况一:Go或Goland架构不对情况二:

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll