当前位置:网站首页>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;}
边栏推荐
- 如何筹备一场感人的婚礼
- 经典模型——ResNet
- Utonmos: digital collections help the inheritance of Chinese culture and the development of digital technology
- Arthas(阿尔萨斯) 能为你做什么?
- Cliquez sur le bouton action de la liste pour passer à une autre page de menu et activer le menu correspondant
- ArrayList # sublist these four holes, you get caught accidentally
- golang正則regexp包使用-06-其他用法(特殊字符轉換、查找正則共同前綴、切換貪婪模式、查詢正則分組個數、查詢正則分組名稱、用正則切割、查詢正則字串)
- 路由跳轉之點擊列錶的操作按鈕,跳轉至另一個菜單頁面並激活相應的菜單
- [system architecture] - how to evaluate software architecture
- Is it safe to open an online stock account?
猜你喜欢
[solution] the blue screen restart problem of the virtual machine started by the VMware of Lenovo Savior
Learn Tai Chi Maker - mqtt (IV) server connection operation
Using meta analysis to drive the development of educational robot
Clion项目中运行多个main函数
Analysis of the multiple evaluation system of children's programming
Gd32 ADC acquisition voltage
计组笔记——CPU的指令流水
【哈希表】改进,拉链法哈希结构——直接用两个索引查找,不用每次都hash和%一遍
《你不可不知的人性》经典语录
【读点论文】FBNetV3: Joint Architecture-Recipe Search using Predictor Pretraining 网络结构和超参数全当训练参数给训练了
随机推荐
GStreamer allocator and pool
Good news | congratulations on the addition of 5 new committers in Apache linkage (incubating) community
渐变
Learn Tai Chi Maker - mqtt (IV) server connection operation
R language Markov chain Monte Carlo: practical introduction
计组笔记——CPU的指令流水
MySQL增删查改(进阶)
[QT] custom control - air quality dashboard
在UE内添加控制台工程(Programs)
Utonmos adheres to the principle of "collection and copyright" to help the high-quality development of traditional culture
Inkscape如何将png图片转换为svg图片并且不失真
小程序或者for循序要不要加key?
Scratch returns 400
[QT] custom control - switch
360 秒了解 SmartX 超融合基础设施
经典模型——AlexNet
《你不可不知的人性》經典語錄
Is it safe to open an account in flush online? How to open a brokerage account online
多媒体元素,音频、视频
ORB-SLAM系列论文翻译笔记