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 ...
随机推荐
- MyEclipse复制js文件乱码
MyEclipse复制js文件乱码 右击js文件:
- shell怎么判断两个文件内容是否相同
#cat diff_two_file#/bin/sbinfile1=/mnt/mmc/test/aafile2=/mnt/mmc/test/bbdiff $file1 $file2 > /dev ...
- django面试五
http和https的区别https协议需要到ca申请证书,一般免费证书很少,需要交费. 注:CA - certificate authority,身份认证,权威机构认证,CA认证: http是超文本 ...
- Python基础4--一看就会的选择与循环
1 选择 if elif else 注意后面均有: if age>18: print 'adult' elif age>6: print 'teenager' else: print 'k ...
- async/await学习笔记
async/await 的目的是简化使用 promises 的写法. 让我们来看看下面的例子: // 一个标准的 JavaScript 函数 function getNumber1() { r ...
- 常见Web应用程序漏洞
不完善的身份验证措施 .这类漏洞包括应用程序登录机制中的各种缺陷,可能会使攻击者破解保密性不强的密码.发动蛮力攻击或完全避开登录. 不完善的访问控制措施.这一问题涉及的情况包括:应用程序无法为数据和功 ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - MySQL
文章目录 1. 环境依赖 2. 数据源3. 脚本初始化 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 4. 使用JdbcTemplate操作5. 总结 4.1. ...
- undefined is not an object (evaluating '_react2.PropTypes.string')
对所引用的组件原 .import React, {Component,PropTypes} from 'react' 改成:import React, {Component} from 'react' ...
- linux shell 中文件编码查看及转换方法
参考: http://edyfox.codecarver.org/html/vim_fileencodings_detection.html 一.查看文件编码. 在打开文件的时候输入:set ...
- [LeetCode&Python] Problem 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...