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 ...
随机推荐
- debian系统下安装ssh
SSH 为 Secure Shell 的缩写,SSH 为建立在应用层基础上的安全协议.SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议.利用 SSH 协议可以有效防止远程管理过程中 ...
- 八. Python基础(8)--函数
八. Python基础(8)--函数 1 ● 函数返回布尔值 注意, 自定义的函数也可以是用来作逻辑判断的, 例如内置的startswith()等函数. def check_len(x): ' ...
- Android : SELinux 简析&修改
一 SELinux背景知识 SELinux出现之前,Linux上的安全模型叫DAC,全称是Discretionary Access Control,翻译为自主访问控制.DAC的核心思想很简单,就是: ...
- 【Appium】Appium工作原理
参考:http://www.cnblogs.com/zhjsll/p/5698878.html 原作者写的很好,所以直接放在这里. 一.什么是Appium Appium是一个开源.跨平台的测试框架,可 ...
- RocketMq源码学习(一) nameService
public class NamesrvStartup { public static Properties properties = null; public static CommandLine ...
- wait_activity
wait_activity(self, activity, timeout, interval=1): android特有的 返回的True 或 False :Agrs: - activity - 需 ...
- C++连接mysql数据库的两种方法
本文主要介绍了C++连接mysql数据库的两种方法,希望通过本文,能对你有所帮助,一起来看. 现在正做一个接口,通过不同的连接字符串操作不同的数据库.要用到mysql数据库,以前没用过这个数据库,用a ...
- $_SERVER['URI']
WordPress通过301重定向实现非首先域(非www)跳转向本来是一个很简单事情,由于没弄清楚$_SERVER['HTTP_X_REWRITE_URL'] 和$_SERVER['REQUEST_U ...
- ChinaCock让Android App应用不锁屏
<meta-data android:name="keepScreenOn" android:value="true"> </meta-dat ...
- 如何HACK无线家用警报器?
30年前,报警器都是硬连线的,具有分立元件,并由钥匙开关操作.20年前,他们已经演变为使用微控制器,LCD和键盘,但仍然是硬连线.10年前,无线报警器开始变得普及,并增加了许多之前没有的功能. 而如今 ...