当前位置:网站首页>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’.
边栏推荐
- Unity Render Streaming通过Js与Unity自定义通讯
- [机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心
- 106. How to improve the readability of SAP ui5 application routing URL
- Resource Cost Optimization Practice of R & D team
- Red Hat Satellite 6:更好地管理服务器和云
- Go language unit test 3: go language uses gocovey library to do unit test
- JS 将伪数组转换成数组
- 父亲和篮球
- php 迷宫游戏
- Field problems in MySQL
猜你喜欢

8皇后问题

Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)

Logback log sorting

Ocean CMS vulnerability - search php

HALCON联合C#检测表面缺陷——HALCON例程autobahn

Libuv库 - 设计概述(中文版)

The solution of Chinese font garbled code in keil5

Screenshot of the operation steps of upload labs level 4-level 9

Go language web development series 27: Gin framework: using gin swagger to implement interface documents
![[技术发展-24]:现有物联网通信技术特点](/img/f3/a219fe8e7438b8974d2226b4c3d4a4.png)
[技术发展-24]:现有物联网通信技术特点
随机推荐
CVPR 2022 | 美团技术团队精选6篇优秀论文解读
Golang — template
Universal dividend source code, supports the dividend of any B on the BSC
又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
HALCON联合C#检测表面缺陷——HALCON例程autobahn
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Can newly graduated European college students get an offer from a major Internet company in the United States?
Leetcode-1175. Prime Arrangements
Resolved (error in viewing data information in machine learning) attributeerror: target_ names
untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
TensorBoard可视化处理案例简析
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
【556. 下一个更大元素 III】
SQL Injection (AJAX/JSON/jQuery)
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
Asp. Net core1.1 without project JSON, so as to generate cross platform packages
Software testing is so hard to find, only outsourcing offers, should I go?
顺序表(C语言实现)
掌握Cypress命令行选项,是真正掌握Cypress的基础