C++--第13课 - 操作符重载 - 下
第13课 - 操作符重载 - 下
思考:
通过operator关键字能够将操作符定义为全局函数,操作符重载的本质就是函数重载。类的成员函数是否可以作为操作符重载的函数?
1. operator+的成员函数实现
#include <cstdlib>
#include <iostream>
using namespace std;
class Complex
{
int a;
int b;
public:
Complex(int a, int b)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
Complex operator+ (const Complex& c2);
//看上去只有一个参数,实际有俩,另一个是this指针
friend ostream& operator<< (ostream& out, const Complex& c);
};
ostream& operator<< (ostream& out, const Complex& c)
{
out<<c.a<<" + "<<c.b<<"i";
return out;
}
Complex Complex::operator+ (const Complex& c2)
{
Complex ret(0, 0);
ret.a = this->a + c2.a;
ret.b = this->b + c2.b;
return ret;
}
int main(int argc, char *argv[])
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2;
cout<<c1<<endl;
cout<<c2<<endl;
cout<<c3<<endl;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
运行结果:
1 + 2i
3 + 4i
4 + 6i
用成员函数重载的操作符,比全局操作符重载函数少一个参数,即左操作数。另外也不需要使用friend关键字。
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2;
等价于
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1.operator+ c2;
操作符的重载实际上就都是函数来实现的。
问:
什么时候使用全局函数重载操作符?什么时候使用全局函数重载操作符?
答:
l 当无法修改左操作数的类时,使用全局函数进行重载。
l =, [], ()和->操作符只能通过成员函数进行重载。
2. 代码—数组的改进
重载赋值操作符,重载数组操作符,重载比较操作符。
C++编译器会为每个类提供默认的赋值操作符。默认的赋值操作符只是做简单的值复制。
类中存在指针成员变量时就需要重载赋值操作符。
Array.h
#ifndef _ARRAY_H_
#define _ARRAY_H_
class Array
{
private:
int mLength;
int* mSpace;
public:
Array(int length);
Array(const Array& obj);
int length();
~Array();
int& operator[](int i);
Array& operator= (const Array& obj);
bool operator== (const Array& obj);
bool operator!= (const Array& obj);
};
#endif
Array.cpp
#include <stdio.h>
#include "Array.h"
Array::Array(int length)
{
if( length < 0 )
{
length = 0;
}
mLength = length;
mSpace = new int[mLength];
}
Array::Array(const Array& obj)
{
mLength = obj.mLength;
mSpace = new int[mLength];
for(int i=0; i<mLength; i++)
{
mSpace[i] = obj.mSpace[i];
}
}
int Array::length()
{
return mLength;
}
Array::~Array()
{
mLength = -1;
printf("%08X\n", mSpace);
delete[] mSpace;
}
int& Array::operator[](int i)
{
return mSpace[i];
}
Array& Array::operator= (const Array& obj)
{
delete[] mSpace;
mLength = obj.mLength;
mSpace = new int[mLength];
for(int i=0; i<mLength; i++)
{
mSpace[i] = obj.mSpace[i];
}
return *this;
}
bool Array::operator == (const Array& obj)
{
bool ret = true;
if( mLength == obj.mLength )
{
for(int i=0; i<mLength; i++)
{
if( mSpace[i] != obj.mSpace[i] )
{
ret = false;
break;
}
}
}
else
{
ret = false;
}
return ret;
}
bool Array::operator!= (const Array& obj)
{
return !(*this == obj);
}
main.c
#include <stdio.h>
#include "Array.h"
int main()
{
Array a1(10);
Array a2(0);
Array a3(0);
if( a1 != a2 )
{
printf("a1 != a2\n");
}
for(int i=0; i<a1.length(); i++)
{
a1[i] = i + 1;
}
for(int i=0; i<a1.length(); i++)
{
printf("Element %d: %d\n", i, a1[i]);
}
a3 = a2 = a1;
if( a1 == a2 )
{
printf("a1 == a2\n");
}
for(int i=0; i<a2.length(); i++)
{
printf("Element %d: %d\n", i, a2[i]);
}
printf("Press any key to continue...");
getchar();
return 0;
}
3. 要点
C++编译器会为每个类提供默认的赋值操作符。
默认的赋值操作符只是做简单的值复制。
类中存在指针成员变量时就需要重载赋值操作符。
l ++操作符的重载(--操作符是一样的)
++操作符只有一个操作数.
++操作符有前缀和有后缀的区分
l 如何重载++操作符才能区,分前置运算和后置运算?
操作符重载是通过函数重载实现的,C++中通过一个占位参数来区分前置运算和后置运算。
#include <cstdlib>
#include <iostream>
using namespace std;
class Complex
{
int a;
int b;
public:
Complex(int a, int b)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
Complex operator+ (const Complex& c2);
Complex operator++ (int); // obj++
Complex& operator++(); // ++obj
friend ostream& operator<< (ostream& out, const Complex& c);
};
ostream& operator<< (ostream& out, const Complex& c)
{
out<<c.a<<" + "<<c.b<<"i";
return out;
}
Complex Complex::operator++ (int) //具体的实现
{
Complex ret = *this; //拷贝构造函数,将当前的值拷贝到ret中。
a++;
b++;
return ret;
}
Complex& Complex::operator++() //具体的实现
{
++a;
++b;
return *this;
}
//以上连个函数说明前置的++效率更高。
Complex Complex::operator+ (const Complex& c2)
{
Complex ret(0, 0);
ret.a = this->a + c2.a;
ret.b = this->b + c2.b;
return ret;
}
int main(int argc, char *argv[])
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c2;
c2++;
++c3;
cout<<c1<<endl;
cout<<c2<<endl;
cout<<c3<<endl;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
4. 为什么不能重载&&和||操作符
从语法的角度可以重载,但是最好不要,容易出错。
#include <cstdlib>
#include <iostream>
using namespace std;
class Test
{
int i;
public:
Test(int i)
{
this->i = i;
}
Test operator+ (const Test& obj) //加法操作符重载
{
Test ret(0);
cout<<"Test operator+ (const Test& obj)"<<endl;
ret.i = i + obj.i;
return ret;
}
bool operator&& (const Test& obj) //与运算操作符重载
{
cout<<"bool operator&& (const Test& obj)"<<endl;
return i && obj.i;
}
};
int main(int argc, char *argv[])
{
int a1 = 0;
int a2 = 1;
if( a1 && (a1 + a2) )
{
cout<<"Hello"<<endl;
}
Test t1 = 0;
Test t2 = 1;
if( t1 && (t1 + t2) )
{
cout<<"World"<<endl;
}
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
运行结果:
Test operator+ (const Test& obj)
bool operator&& (const Test& obj)
l &&和||是C++中非常特殊的操作符。
l &&和||内置实现了短路规则。
l 操作符重载是靠函数重载来完成的。
l 操作数作为函数参数传递。
l C++的函数参数都会被求值,无法实现短路规则。
小结:
操作符重载可以直接使用类的成员函数实现。
=, [], ()和->操作符只能通过成员函数进行重载。
++操作符通过一个int参数进行前置与后置的重载。
C++中不要重载&&和||操作符。
转载于:https://www.cnblogs.com/free-1122/p/11336208.html
C++--第13课 - 操作符重载 - 下相关推荐
- Python学习手册之内部方法、操作符重载和对象生命周期
在上一篇文章中,我们介绍了 Python 的类和继承,现在我们介绍 Python 的内部方法.操作符重载和对象生命周期. 查看上一篇文章请点击:https://www.cnblogs.com/dust ...
- 2021-08-13c++——类之操作符重载
+号重载 1)成员函数重载 如果要返回Person类,构造Person的时候就一定要赋初始值. 如果不赋予初始值,就一定要有默认初始值,因为提供了含参构造函数,编译器就不会再提供默认构造函数了. #i ...
- delphi 操作符重载_Delphi XE2中的运算符重载示例
delphi 操作符重载 In my programming career I have only very rarely run into situations where operator ove ...
- python中线程安全的数据结构_Scala(八)-①-数据结构-集合操作-线程安全的集合-操作符重载...
① 集合操作 Why 为什么需要集合操作?集合操作都包括哪些?Scala的集合操作主要为了适应大数据的发展,我们以Map为例.于事需入局,于程需入题,先看下题. 入题 请将list(3,5,7) 中的 ...
- C++拾趣——有趣的操作符重载
操作符重载是C++语言中一个非常有用的特性.它可以让我们比较优雅的简化代码,从而更加方便的编写逻辑. 为什么要使用操作符重载 一种常见的用法是重载<<运算符,让标准输出可以输出自定义的类型 ...
- 操作符重载——C/C++学习笔记
此篇文章来自于网上,作为自己学习中的笔记,若有侵权行为,请告之,24小时之内必删除!下面就转入正题吧! 一.什么是操作符重载? 一看到重载,很容易就让人联想到成员函数重载,函数重载可以使名称相同的函数 ...
- C++——构造函数(拷贝构造,拷贝复制),析构函数,操作符重载
C++--构造函数(拷贝构造,拷贝复制),析构函数,操作符重载 构造函数与析构函数:: 涉及构造函数还可以看这篇文章C++搞懂深拷贝初始化=与赋值=的区别 1.声明和定义构造函数和析构函数 构造函数在 ...
- C++中逗号操作符重载的分析
1,关注逗号操作符重载后带来的变化: 2,逗号操作符: 1,逗号操作符(,)可以构成都好表达式:exp1, exp2, exp3, ..., expN 1,逗号表达式用于将多个表达式连接为一个表达式: ...
- C++中的逻辑操作符重载
文章目录 1 C++中的逻辑操作符重载 1.1 逻辑操作符的原生语义 1.2 重载逻辑操作符 1.3 逻辑操作符重载的建议 1 C++中的逻辑操作符重载 1.1 逻辑操作符的原生语义 逻辑操作符的原生 ...
- Python3 操作符重载方法
操作符重载方法: 类(class)通过使用特殊名称的方法(len(self))来实现被特殊语法(len())的调用 类(class)通过使用特殊名称的方法(len(self))来实现被特殊语法(len ...
最新文章
- MIna框架I/O Service层设计
- Android文件Apk下载变ZIP压缩包解决方案
- 事务的隔离级别 mysql
- android activity查询,android中activity.findViewById()方法查找的是什么?
- Java关于数据结构的实现:散列
- 使用localhost调试本地代码,setcookie无效
- 基于模拟退火算法解决TSP问题 | MATLAB源码
- linux ps的a选项,linux下PS命令详解(转载)
- 新手如何制作专业的思维导图
- 宽带波束形成及MATLAB实现
- Java ques: java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
- 三级联动(原生js)
- 汉语语法研究参考文献
- uni-app常见的生命周期
- unity支持的模型数据格式,unity3d模型制作规范
- iOS之券商唯品会接入总结
- 【Delphi】中使用消息Messages(八)Android 系统消息感知
- MySQL 5.5的安装配置(保姆级别,超级简单)
- 为啥app没有menu键?
- 汇聚150万开发者,华为云致力于成为最佳应用构建平台
热门文章
- 实习踩坑之路:利用Java8新特性实现不同范型List之间的相互转换
- Flink on Zeppelin (3) - Streaming 篇
- MAC安装apache tomcat配置方法图文教程
- 插件开发之360 DroidPlugin源码分析(五)Service预注册占坑
- python标准库——time模块
- java七武器系列_Java七武器系列多情环 --多功能Profiling工具 JVisual VM
- nacos动态配置数据源_Spring Cloud 系列之 Alibaba Nacos 配置中心
- windows关于python虚拟机的设置以及安装使用virtualenv
- python 线程-threding示例使用
- python-gui-pyqt5的使用方法-6--lambda传递参数的方法: