当前位置:网站首页>Worm copy construction operator overload
Worm copy construction operator overload
2022-06-26 03:18:00 【Hua Weiyun】
Class 6 Default member functions
If there are no members in a class , It is called empty class for short . Is there nothing in the empty class ? It's not ,== Any class we don't write , Will automatically generate the following 6 Default member functions ==
copy constructor
Constructors : Only a single parameter , The formal parameter is a reference to an object of this type ( Commonly used const modification ), Automatically called by the compiler when creating a new object with an existing class type object .
features
== copy constructor == It is also a special member function , Its characteristics are as follows :
- The copy constructor is == An overloaded form of constructor ==.
- Copy constructor == There is only one parameter == And == Reference must be used to pass parameters ==, Use == The value passing method will cause infinite recursive calls ==.
- == If no definition is displayed , The system generates the default copy constructor ==. The default copy constructor object is stored in memory == Complete the copy in byte order ==, This copy is called == Shallow copy ==, Or copy .
== But this will cause some problems ==
== When there is a resource point, you can't use shallow copy ==
Assignment operator overload
Operator overloading
==C++ In order to enhance the readability of the code, operator overloading is introduced , Operator overloading is a function with a special function name ,== It also has its return value type , Function name and parameter list , Its return value type and parameter list are similar to ordinary functions .
== The function name is : keyword operator Followed by operator symbols that need to be overloaded ==.
== The function prototype : return type operator The operator ( parameter list )==
Be careful :
- Cannot pass == Connect other symbols == To create a new operator : such as [email protected]
- The overload operator must have an operand of class type or enumeration type
- Operators for built-in types , Its meaning cannot be changed , for example : Built in integer +, No Can change its meaning
- When overloading a function as a member of a class , Its parameters look less than the operands 1 The operator of a member function has a default formal parameter this, Limited to the first parameter
- .* 、:: 、sizeof 、?: 、. Pay attention to the above 5 Operators cannot be overloaded . This often appears in multiple-choice questions in written tests
== Implementation of operator overloading ==
// 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) { }private: int _year; int _month; int _day;};int main(){ Date d1(2022, 1, 1); Date d2(2022, 1, 1); cout << (d1 == d2) << endl; cout << (d1 < d2) << endl; return 0;}
边栏推荐
- Use annotationdbi to convert gene names in R
- 虫子 构造与析构
- 解析社交机器人中的技术变革
- Additional: brief description of hikaricp connection pool; (I didn't go deep into it, but I had a basic understanding of hikaricp connection pool)
- 【论文笔记】Manufacturing Control in Job Shop Environments with Reinforcement Learning
- QT compilation error: unknown module (s) in qt: script
- jupyter notebook的插件安装以及快捷键
- [reading papers] fbnetv3: joint architecture recipe search using predictor training network structure and super parameters are all trained by training parameters
- Components and routing
- 用元分析法驱动教育机器人的发展
猜你喜欢

MySQL开发环境

2021-08-04

Analysis on the diversification of maker space mechanism construction

分割、柱子、list

Problems encountered in project deployment - production environment

Google recommends using kotlin flow in MVVM architecture

Analysis of technological changes in social robots

《你不可不知的人性》经典语录

Notes on the 3rd harmonyos training in the studio

Drawing structure diagram with idea
随机推荐
网络PXE启动WinPE,支持UEFI和LEGACY引导
给网站添加“开放搜索描述“以适配浏览器的“站点搜索“
R language Markov chain Monte Carlo: practical introduction
显卡、GPU、CPU、CUDA、显存、RTX/GTX及查看方式
Route jump: click the operation button of the list to jump to another menu page and activate the corresponding menu
gstreamer分配器与pool的那些事
Review of the paper: unmixing based soft color segmentation for image manipulation
Une citation classique de la nature humaine que vous ne pouvez pas ignorer
Mysql常用sql语句之修改表名、删除表、获取表信息、删除指定日期的表记录
Notes on the 3rd harmonyos training in the studio
Redis configuration and optimization of NoSQL
QT compilation error: unknown module (s) in qt: script
360 秒了解 SmartX 超融合基础设施
Distributed e-commerce project grain mall learning notes < 3 >
Utonmos: digital collections help the inheritance of Chinese culture and the development of digital technology
经典模型——AlexNet
微信小程序开发准备工作
MySQL数据库基础
计组笔记 数据表示与运算 校验码部分
UE5全局光照系统Lumen解析与优化








