当前位置:网站首页>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’.
边栏推荐
- Kivy教程之 如何通过字符串方式载入kv文件设计界面(教程含源码)
- [技术发展-24]:现有物联网通信技术特点
- Unity render streaming communicates with unity through JS
- [understanding by chance-37]: the structure of human sensory system determines that human beings are self-centered
- Libuv库 - 设计概述(中文版)
- Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
- Flutter动态化 | Fair 2.5.0 新版本特性
- Static linked list (subscript of array instead of pointer)
- 使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
- Spark practice 1: build spark operation environment in single node local mode
猜你喜欢

Unity embeddedbrowser browser plug-in event communication

Go language web development series 26: Gin framework: demonstrates the execution sequence of code when there are multiple middleware

Road construction issues

Complete deep neural network CNN training with tensorflow to complete picture recognition case 2

Mycms we media mall v3.4.1 release, user manual update

Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock

CVPR 2022 | 美团技术团队精选6篇优秀论文解读

logback日志的整理

Multi table query of MySQL - multi table relationship and related exercises

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
随机推荐
mysql中的字段问题
Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
SwiftUI 开发经验之作为一名程序员需要掌握的五个最有力的原则
The principle of human voice transformer
This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
[556. Next larger element III]
树的深入和广度优先遍历(不考虑二叉树)
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
Heap structure and heap sort heapify
SQL Injection (GET/Select)
The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
Students who do not understand the code can also send their own token, which is easy to learn BSC
Resolved (error in viewing data information in machine learning) attributeerror: target_ names
Static linked list (subscript of array instead of pointer)
8皇后问题
Libuv库 - 设计概述(中文版)
使用vscode查看Hex或UTF-8编码
JSON serialization case summary
SQL Injection (AJAX/JSON/jQuery)
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon