当前位置:网站首页>Insect operator overloaded a fun
Insect operator overloaded a fun
2022-06-26 13:48:00 【Hua Weiyun】
Let's have fun
== Array class ==
== You don't quote back words , You cannot modify the object ==
== Not only can you reduce the number of copies by referring to the parameter , You can also modify objects ==
class Array{public: Array() { for (auto& e : _a) { e *= 10; } } // Copy structure print Array(Array& a) { cout << "Array" << endl; } ~Array() { } //[] Square bracket dereference is also an operator int& operator[](size_t pos) { // visit pos Data from that location 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;}
Assignment operator overload
Assignment operators mainly have four points :
- Parameter type
- Return value
- Check whether you assign values to yourself
- return *this
- If a class does not explicitly define an assignment operator overload , The compiler will also generate a , Complete the value copy of objects in byte order .
== Chained assignment copy ==
== But it doesn't rule out that some people assign values to themselves ==
that == The default assignment overloaded function generated by the compiler can already complete the value copy of byte order == 了 , Do we still need to do it ourselves ?
// Date class class Date{public: // Constructor does not write , The compiler automatically generates constructors , So the constructor is also the default member function Date(int year = 2022, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } // We don't write copy constructs , The compiler will automatically generate a default copy construct // Copy structure /*Date(const Date& d) { _year = d._year; _month = d._month; _day = d._day; }*/ void Print() { cout << _year << "-" << _month << "-" << _day << endl; } // Judge whether the dates are equal bool operator==(Date x2) { return _year == x2._year && _month == x2._month && _day == x2._day; } // Judge whether the left is smaller than the right 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 plus days The returned date is still Date operator+(int day) { } // Date minus days The returned date is still Date operator-(int day) { } // Date minus date Days returned int operator-(Date d2) { } // Copy of assignment 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(); // It's also a copy behavior , But the difference is , Copy construction is when you create an object , Take a copy of the initialization of similar objects // Both objects exist when assigning copies here , Are initialized , Now I want to copy the assignment from one object to another d1 = d1; d1 = d2 = d3; d1.Print(); d2.Print(); d3.Print(); return 0;}
A fun one
边栏推荐
- Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached
- 7.Consul服务注册与发现
- Mysql database explanation (6)
- 古瑞瓦特沖刺港交所上市:創下“多個第一”,獲IDG資本9億元投資
- Beifu realizes the control of time slice size and quantity through CTU and ton
- Design of PHP asymmetric encryption algorithm (RSA) encryption mechanism
- 泰山OFFICE技术讲座:使用字体粗体的四种情形
- Mongodb series window environment deployment configuration
- PHP非对称加密算法(RSA)加密机制设计
- Log in to the server using SSH key pair
猜你喜欢

三维向量的夹角

去某东面试遇到并发编程问题:如何安全地中断一个正在运行的线程

Design of simple digital circuit traffic light

HW蓝队溯源流程详细整理

VTK 圆柱体的生成与渲染

Detailed introduction to shell script (4)

Analysis of state transition diagram of Beifu NC axis

Nexys A7开发板资源使用技巧

ES6:Map
![[MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system](/img/03/a1885e4740bbfdbdee2446e3dd81d0.png)
[MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system
随机推荐
awk工具
Generate JDE dot train
KITTI Tracking dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
Exercise set 1
7-16 monetary system I
Pytorch based generation countermeasure Network Practice (7) -- using pytorch to build SGAN (semi supervised GaN) to generate handwritten digits and classify them
8.Ribbon负载均衡服务调用
ES6 module
Connection migration for DataGrid configuration
Jenkins build prompt error: eacces: permission denied
Included angle of 3D vector
PHP非对称加密算法(RSA)加密机制设计
Es common grammar I
Beifu realizes the control of time slice size and quantity through CTU and ton
Free machine learning dataset website (6300+ dataset)
Ring queue PHP
Analysis of state transition diagram of Beifu NC axis
Taishan Office Technology Lecture: four cases of using bold font
imagecopymerge
Some conclusions about Nan








