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的更多相关文章

  1. 可空类型(Nullable<T>)及其引出的关于explicit、implicit的使用

    问题一:Nullable<T>可赋值为null 先看两行C#代码 int? i1 = null; int? i2 = new int?(); int? 即Nullable<int&g ...

  2. 关于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\ 这个问题出现没 ...

  3. 显示转换explicit和隐式转换implicit

    用户自定义的显示转换和隐式转换 显式转换implicit关键字告诉编译器,在源代码中不必做显示的转型就可以产生调用转换操作符方法的代码. 隐式转换implicit关键字告诉编译器只有当源代码中指定了显 ...

  4. explicit抑制隐型转换

    本文出自 http://www.cnblogs.com/cutepig/ 按照默认规定,只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应数据类型的数据转换为该类对象,如下面所示: clas ...

  5. C++ explicit关键字详解

    本文系转载,原文链接:http://www.cnblogs.com/ymy124/p/3632634.html 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用 ...

  6. Implicit and Explicit Multithreading MULTITHREADING AND CHIP MULTIPROCESSORS

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The concept of thread ...

  7. 【Android 疑难杂症1】android.content.ActivityNotFoundException: Unable to find explicit activity class

    android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.cnote ...

  8. C++笔记(1)explicit构造函数

    按照默认规定,只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应数据类型的数据转换为该类对象,如下面所示: class String { String ( const char* p );  ...

  9. C++中explicit关键字的使用

    看书看到了explicit关键字,就来做个笔记,讲得比较明白,比较浅. 在C++中,我们有时可以将构造函数用作自动类型转换函数.但这种自动特性并非总是合乎要求的,有时会导致意外的类型转换,因此,C++ ...

  10. 错误: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 ...

随机推荐

  1. SQL-6查找所有员工入职时候的薪水情况,给出emp_no以及salary, 并按照emp_no进行逆序

    题目描述 查找所有员工入职时候的薪水情况,给出emp_no以及salary, 并按照emp_no进行逆序CREATE TABLE `employees` (`emp_no` int(11) NOT N ...

  2. Fedora的一些个人配置

    0,老传统 yum install screenfetch 1,关闭蜂鸣器 edit /etc/bashrc setterm -blength 0#setterm -bfreq 10 #这个可以设置声 ...

  3. python中的运算符归类

    运算符 目标 算数运算符 比较(关系)运算符 逻辑运算符 赋值运算符 成员运算符 运算符的优先级 数学符号表链接:https://zh.wikipedia.org/wiki/数学符号表 01. 算数运 ...

  4. 5.Python爬虫入门五之URLError异常处理

    大家好,本节在这里主要说的是URLError还有HTTPError,以及对它们的一些处理. 1.URLError 首先解释下URLError可能产生的原因: 网络无连接,即本机无法上网 连接不到特定的 ...

  5. 性能测试-10.数据分析Analysis

    Analysis Summary 平均响应时间(Average TransactionResponse Time) 每秒响应数(Transactions per Second) 1.Vuser  Ru ...

  6. JavaBean转化为Map,List<JavaBean>转化为List<Map>

    /** * 将对象转化为map * * @param bean * @param <T> * @return */ private <T> Map<String, Obj ...

  7. nodejs的express框架(request,response方法汇总)

    Response 对象 - response 对象表示 HTTP 响应,即在接收到请求时向客户端发送的 HTTP 响应数据.常见属性有: res.app:同req.app一样 res.append() ...

  8. Unity游戏开发常用的一些函数用法

    Unity游戏开发常用函数 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享. ...

  9. Unity 3D接入ShareSDK流程2

    Unity开发VR之Vuforia 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...

  10. react写单选按钮或table标签

    首先,原理是一样的: class Loca_choose_wrap extends Component{ constructor(){ super(); this.state={ port_name: ...