当前位置:网站首页>虫子 运算符重载的一个好玩的
虫子 运算符重载的一个好玩的
2022-06-26 13:06:00 【华为云】
我们玩个好玩的
==数组类==
==你不引用返回的话,就不可以修改对象==
==引用传参的话不仅可以减少拷贝,还可以修改对象==
class Array{public: Array() { for (auto& e : _a) { e *= 10; } } //拷贝构造打印 Array(Array& a) { cout << "Array" << endl; } ~Array() { } //[]方括号解引用也是运算符 int& operator[](size_t pos) { //访问pos那个位置的数据 return _a[pos]; }private: int _a[10] = {0,1,2,3,4,5,6,7,8,9};};int main(){ Array a; cout << a[0] << endl; cout << a[1] << endl; cout << a[2] << endl; cout << a[3] << endl; a[0] = 100; a[1] = 100; a[2] = 100; a[3] = 100; cout << a[0] << endl; cout << a[1] << endl; cout << a[2] << endl; cout << a[3] << endl; return 0;}
赋值运算符重载
赋值运算符主要有四点:
- 参数类型
- 返回值
- 检测是否自己给自己赋值
- 返回*this
- 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝。
==链式赋值拷贝==
==但是不排除一些人自己给自己赋值==
那么==编译器生成的默认赋值重载函数已经可以完成字节序的值拷贝==了,我们还需要自己实现吗?
//日期类class Date{public: //构造函数不写,编译器会自动生成构造函数,所以构造函数也是默认成员函数 Date(int year = 2022, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } //我们不写拷贝构造,编译器会自动生成默认的拷贝构造 //拷贝构造 /*Date(const Date& d) { _year = d._year; _month = d._month; _day = d._day; }*/ void Print() { cout << _year << "-" << _month << "-" << _day << endl; } //判断日期是否相等 bool operator==(Date x2) { return _year == x2._year && _month == x2._month && _day == x2._day; } //判断左边是否小于右边 bool operator<(Date x2) { if (_year < x2._year) return true; else if(_year == x2._year) { if (_month < x2._month) return true; else if(_month == x2._month) { if (_day < x2._day) return true; } } return false; } //日期加天数 返回的还是日期 Date operator+(int day) { } //日期减天数 返回的还是日期 Date operator-(int day) { } //日期减日期 返回的是天数 int operator-(Date d2) { } //赋值拷贝 d1 = d1; Date& operator=(const Date& d) { if (this != &d) { _year = d._year; _month = d._month; _day = d._day; } return *this; }private: int _year; int _month; int _day;};int main(){ Date d1(2022, 1, 1); Date d2; Date d3(2022,5,5); d1.Print(); d2.Print(); d3.Print(); //也是拷贝行为,但是不一样的是,拷贝构造是创建一个对象时,拿同类对象初始化的拷贝 //这里的赋值拷贝时两个对象都存在了,都被初始化过,现在想把一个对象赋值拷贝给另一个对象 d1 = d1; d1 = d2 = d3; d1.Print(); d2.Print(); d3.Print(); return 0;}
一个好玩的
边栏推荐
- 计算两点之间的距离(二维、三维)
- Oplg: new generation cloud native observable best practices
- Taishan Office Technology Lecture: four cases of using bold font
- [how to connect the network] Chapter 2 (next): receiving a network packet
- mysql讲解(一)
- 7-3 minimum toll
- Electron official docs series: Contributing
- Wechat applet -picker component is repackaged and the disabled attribute is added -- below
- Bigint: handles large numbers (integers of any length)
- Beifu PLC model selection -- how to see whether the motor is a multi turn absolute value encoder or a single turn absolute value encoder
猜你喜欢
What features are added to Photoshop 2022 23.4.1? Do you know anything
Basic methods for network diagnosis and hardware troubleshooting of Beifu EtherCAT module
Beifu PLC realizes data power-off maintenance based on cx5130
Cloudcompare - Poisson reconstruction
Es sauvegarde et restauration des données par instantané
三维向量的夹角
免费的机器学习数据集网站(6300+数据集)
Common faults of MySQL database - forgetting database password
Chapter 10 setting up structured logging (2)
Mysql database explanation (III)
随机推荐
Gurivat sprint Harbour Exchange listed: created “multiple first”, received 900 million yuan Investment from IDG capital
古瑞瓦特冲刺港交所上市:创下“多个第一”,获IDG资本9亿元投资
Detailed sorting of HW blue team traceability process
MySQL数据库讲解(五)
HW蓝队溯源流程详细整理
imagecopymerge
H5视频自动播放和循环播放
ES基於Snapshot(快照)的數據備份和還原
Solutions to insufficient display permissions of find and Du -sh
A collection of common tools for making we media videos
Guruiwat rushed to the Hong Kong stock exchange for listing: set "multiple firsts" and obtained an investment of 900million yuan from IDG capital
VTK 圆柱体的生成与渲染
Learn how to develop owl components by hand (7): practical use of owl projects
【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示
MongoDB系列之适用场景和不适用场景
Network remote access using raspberry pie
Traverse the specified directory to obtain the file name with the specified suffix (such as txt and INI) under the current directory
Common faults of MySQL database - forgetting database password
ES6 module
Pytorch based generation countermeasure Network Practice (7) -- using pytorch to build SGAN (semi supervised GaN) to generate handwritten digits and classify them