Explicit
Prefixing the explicit keyword to the constructor prevents the compiler from using that constructor for implicit conversions. Example is shown below.
namespace HW
{
/**
* @class MoneyC
* @brief
*/
class MoneyC
{
public:
/**
* @brief Constructor
*/
MoneyC(); /**
* @brief Constructor
*/
explicit MoneyC(float iValue); /**
* @brief Destructor
*/
~MoneyC() = default; /**
* @brief Set copy constructor as delete to prevent unintentional creation
*/
//MoneyC(const MoneyC& iValue) = delete; float GetAmount(void) const; /**
* @brief Set copy constructor as delete to prevent unintentional creation
*/
MoneyC(const MoneyC& iValue) = delete;
/**
* @brief Set copy assignment as delete to prevent unintentional creation
*/
const MoneyC& operator=(const MoneyC& iValue) = delete;
private:
float mAmount;
};
namespace HW
{ MoneyC::MoneyC():mAmount(1.0)
{
} MoneyC::MoneyC(float iValue):mAmount(iValue)
{
} float MoneyC::GetAmount(void) const
{
return this->mAmount;
} } // end of namespace HW void DisplayMoneyInfo(const HW::MoneyC& iMoney)
{
std::cout << "The Amountt of Money is: ";
std::cout << iMoney.GetAmount() << std::endl; } void MoneyTest(void)
{
std::cout << "===============MoneyTest()==================\n";
HW::MoneyC money1(4.99f);
DisplayMoneyInfo(money1);
DisplayMoneyInfo(4.99f);
DisplayMoneyInfo((HW::MoneyC)7.99f);
}
When compiling, the error will be indicated.
..\src\Learning.cpp:123:26: error: invalid initialization of reference of type 'const HW::MoneyC&' from expression of type 'float'
DisplayMoneyInfo(4.99f);
If remove the “explicit”, it can be compiled successful.
Explicit的更多相关文章
- 可空类型(Nullable<T>)及其引出的关于explicit、implicit的使用
问题一:Nullable<T>可赋值为null 先看两行C#代码 int? i1 = null; int? i2 = new int?(); int? 即Nullable<int&g ...
- 关于Django 错误 doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
记录一下 报错 doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS\ 这个问题出现没 ...
- 显示转换explicit和隐式转换implicit
用户自定义的显示转换和隐式转换 显式转换implicit关键字告诉编译器,在源代码中不必做显示的转型就可以产生调用转换操作符方法的代码. 隐式转换implicit关键字告诉编译器只有当源代码中指定了显 ...
- explicit抑制隐型转换
本文出自 http://www.cnblogs.com/cutepig/ 按照默认规定,只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应数据类型的数据转换为该类对象,如下面所示: clas ...
- C++ explicit关键字详解
本文系转载,原文链接:http://www.cnblogs.com/ymy124/p/3632634.html 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用 ...
- Implicit and Explicit Multithreading MULTITHREADING AND CHIP MULTIPROCESSORS
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The concept of thread ...
- 【Android 疑难杂症1】android.content.ActivityNotFoundException: Unable to find explicit activity class
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.cnote ...
- C++笔记(1)explicit构造函数
按照默认规定,只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应数据类型的数据转换为该类对象,如下面所示: class String { String ( const char* p ); ...
- C++中explicit关键字的使用
看书看到了explicit关键字,就来做个笔记,讲得比较明白,比较浅. 在C++中,我们有时可以将构造函数用作自动类型转换函数.但这种自动特性并非总是合乎要求的,有时会导致意外的类型转换,因此,C++ ...
- 错误:Implicit super constructor xx() is undefined for default constructor. Must define an explicit constructor
错误:Implicit super constructor xx() is undefined for default constructor. Must define an explicit con ...
随机推荐
- 九. Python基础(9)--命名空间, 作用域
九. Python基础(9)--命名空间, 作用域 1 ● !a 与 not a 注意, C/C++可以用if !a表示if a == 0, 但是Python中只能用if not a来表示同样的意义. ...
- minidebug学习分析 01 基本框架
0x01 基本框架 基本框架就是CreateProcess启动目标程序,再通过调试事件DEBUG_EVENT在调试循环中监控程序的行为. (1)CreatProcess BOOL CreateP ...
- <HBase><读写><LSM>
Overview HBase中的一个big table,首先会按行划分成一些region(这些region之间是有序的,由startkey保证),每个region分配到不同的节点进行存储.因此,reg ...
- Linux:磁盘配额
磁盘配额 一.简略步骤显示 第一步:关闭虚拟机 第二步:编辑虚拟机设置--硬盘--添加 第三步:查看磁盘分区情况 fdisk -l 第四步:选择磁盘分区 fdisk /dev/sda2 第五步:选择磁 ...
- mysql'密码安全
MYSQL数据库的安全配置 MYSQL密码的修改与恢复 MYSQL数据库密码的修改 Mysql5.7以下默认root登录密码为空,安装完成之后首先需要修改root的登录密码. # mysqladm – ...
- ubantu-vim操作
vim其实就是vi的升级版,vi里的所有命令vim里都可以用,一般使用来说几乎没什么差别. 注:本篇文章区分大小写! vi / vim三级模式的关系: 命令行模式 任何时候,不管用户处于何种模式,只要 ...
- DHCP服务配置
DHCP(Dynamic Host Configuration Protocol)动态主机配置协议 -->是由Internet工作任务小组设计开发的,专用于对TCP/IP网络中的计算机自定分配T ...
- 2016ICPC-大连 A Simple Math Problem (数学)
Given two positive integers a and b,find suitable X and Y to meet the conditions: X+Y=a Least Common ...
- FZU 1759-Super A^B mod C
传送门:http://acm.fzu.edu.cn/problem.php?pid=1759 Accept: 1161 Submit: 3892Time Limit: 1000 mSec ...
- C#类中字段封装为属性
本文描述内容转载 https://zhidao.baidu.com/question/1174413218458798139.html 感谢 冥冥有你PD 的解答!!! 问题思索1 类成员包括变量和方 ...