当前位置:网站首页>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’.
边栏推荐
- Windos creates Cordova prompt because running scripts is prohibited on this system
- Kivy教程之 如何通过字符串方式载入kv文件设计界面(教程含源码)
- Road construction issues
- Leetcode-1175.Prime Arrangements
- untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
- Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
- Several common optimization methods matlab principle and depth analysis
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- MyCms 自媒体商城 v3.4.1 发布,使用手册更新
- Unity render streaming communicates with unity through JS
猜你喜欢
Flutter dynamic | fair 2.5.0 new version features
使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
【BW16 应用篇】安信可BW16模组与开发板更新固件烧录说明
Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
logback日志的整理
PhpMyAdmin stage file contains analysis traceability
Halcon combined with C # to detect surface defects -- Halcon routine autobahn
RichView TRVStyle ListStyle 列表样式(项目符号编号)
Comprehensively develop the main channel of digital economy and digital group, and actively promote the utonmos digital Tibet market
常见的几种最优化方法Matlab原理和深度分析
随机推荐
SVN添加文件时的错误处理:…\conf\svnserve.conf:12: Option expected
Spark practice 1: build spark operation environment in single node local mode
[technology development-24]: characteristics of existing IOT communication technology
Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
使用vscode查看Hex或UTF-8编码
Asp. Net core1.1 without project JSON, so as to generate cross platform packages
There is nothing new under the sun. Can the meta universe go higher?
When updating mysql, the condition is a query
Kivy教程之 如何通过字符串方式载入kv文件设计界面(教程含源码)
栈应用(平衡符)
User and group command exercises
Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu
SwiftUI 开发经验之作为一名程序员需要掌握的五个最有力的原则
Golang — template
Go language unit test 3: go language uses gocovey library to do unit test
[understanding by chance-37]: the structure of human sensory system determines that human beings are self-centered
mysql中的字段问题
Halcon combined with C # to detect surface defects -- Halcon routine autobahn
KEIL5出现中文字体乱码的解决方法
SQL Injection (AJAX/JSON/jQuery)