当前位置:网站首页>Conversion function and explicit
Conversion function and explicit
2022-07-03 13:49:00 【yolo_ yyh】
brief introduction :
Conversion functions fall into two categories , One is transfer out , Transform yourself into another type , One is transfer in , Turn others into your own type , When both exist , There may be ambiguity , The compiler cannot recognize , It's time to explicit keyword .
Test code :
First look at the structure of the class :
class Fraction {
public:
Fraction(int num, int den = 1) : m_num(num), m_den(den) { // into
std::cout << "constructor funtion have called" << std::endl;
}
operator double() const { // Transfer out
std::cout << "operator funtion have called" << std::endl;
return ((double)m_num / m_den);
}
Fraction operator+(const Fraction& f) {
this->m_num += f.m_num;
this->m_den += f.m_den;
return *this;
}
void printf() {
std::cout << m_num << "\t" << m_den <<std::endl;
}
private:
int m_num;
int m_den;
};
Calling class 1:
int main() {
Fraction fun(3, 5);
double d = fun + 4;
std::cout << d << std::endl;
return 0;
}
The operation results are as follows :
You can see objects fun Overloaded function called , Transformed into double type .
Calling class 2:
int main() {
Fraction fun(3, 5);
Fraction d = fun + 4;
d.printf();
return 0;
}
You need to comment out Fraction Class operator double() Method , The operation results are as follows :
You can see int Type of 4, Called Fraction Constructor for , Into the Fraction type ;
Then recover Fraction Of operator double() Method , Errors will be reported at compile time , Ambiguous procedure , Add the constructor explicit The key word , You're going to report a mistake conversion from ‘double’ to non-scalar type ‘Fraction’.
边栏推荐
- Replace the GPU card number when pytorch loads the historical model, map_ Location settings
- Spark实战1:单节点本地模式搭建Spark运行环境
- Swiftui development experience: the five most powerful principles that a programmer needs to master
- Depth and breadth first traversal of tree (regardless of binary tree)
- Resolved (error in viewing data information in machine learning) attributeerror: target_ names
- windos 创建cordova 提示 因为在此系统上禁止运行脚本
- The reasons why there are so many programming languages in programming internal skills
- 【BW16 应用篇】安信可BW16模组与开发板更新固件烧录说明
- Heap structure and heap sort heapify
- User and group command exercises
猜你喜欢
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
SQL Injection (POST/Search)
MySQL functions and related cases and exercises
Libuv Library - Design Overview (Chinese version)
RichView TRVStyle ListStyle 列表样式(项目符号编号)
8皇后问题
CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
[email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g"/>
Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
The principle of human voice transformer
随机推荐
研发团队资源成本优化实践
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
RichView TRVStyle ListStyle 列表样式(项目符号编号)
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
使用vscode查看Hex或UTF-8编码
又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
服务器硬盘冷迁移后网卡无法启动问题
Golang — template
Comprehensively develop the main channel of digital economy and digital group, and actively promote the utonmos digital Tibet market
记录关于银行回调post请求405 问题
Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
Golang — template
KEIL5出现中文字体乱码的解决方法
Error running 'application' in idea running: the solution of command line is too long
Libuv库 - 设计概述(中文版)
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
Go language web development series 27: Gin framework: using gin swagger to implement interface documents
Which securities company has the lowest Commission for opening an account online? I want to open an account. Is it safe for the online account manager to open an account
刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?