当前位置:网站首页>虫子 运算符重载的一个好玩的
虫子 运算符重载的一个好玩的
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;}
一个好玩的
边栏推荐
- Basic type of typescript
- Cloudcompare - Poisson reconstruction
- LAMP编译安装
- 【Spark】. Explanation of several icons of scala file in idea
- Wechat applet -picker component is repackaged and the disabled attribute is added -- above
- 三维向量的夹角
- Sed editor
- Nexys A7开发板资源使用技巧
- Gurivat sprint Harbour Exchange listed: created “multiple first”, received 900 million yuan Investment from IDG capital
- Aesthetic experience (episode 238) Luo Guozheng
猜你喜欢

Basic type of typescript

MySQL explanation (I)

古瑞瓦特沖刺港交所上市:創下“多個第一”,獲IDG資本9億元投資

网络远程访问的方式使用树莓派

Nexys A7开发板资源使用技巧

Beifu PLC realizes data power-off maintenance based on cx5130

Mongodb series window environment deployment configuration

Ring queue PHP

NVM installation tutorial

Use of wangeditor rich text editor
随机推荐
MediaPipe手势(Hands)
VTK 圆柱体的生成与渲染
CloudCompare——泊松重建
Bifu divides EtherCAT module into multiple synchronization units for operation -- use of sync units
d的is表达式
Mediapipe gestures (hands)
Beifu PLC based on NT_ Shutdown to realize automatic shutdown and restart of controller
Awk tools
【Proteus仿真】Arduino UNO按键启停 + PWM 调速控制直流电机转速
Beifu PLC realizes zero point power-off hold of absolute value encoder -- use of bias
[how to connect the network] Chapter 2 (Part 1): establish a connection, transmit data, and disconnect
Update and download of Beifu EtherCAT XML description file
[MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system
使用 Performance 看看浏览器在做什么
嵌入式virlog代码运行流程
Basic configuration and test of Beifu twincat3 NCI in NC axis interface
[path of system analyst] Chapter 15 double disk database system (database case analysis)
shell脚本详细介绍(四)
Connection migration for DataGrid configuration
mysql配置提高数据插入效率








