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. SecureCRT使用(转)

      功能介绍 连接概述    1.当您启动SecureCRT时,您将看到“快速连接”对话框与主SecureCRT窗口一起出现.  2.有几种接口可用于连接远程机器:快速连接对话框,工具栏上的连接栏,会 ...

  2. 11.Python-第三方库requests详解(三)

    Response对象 使用requests方法后,会返回一个response对象,其存储了服务器响应的内容,如上实例中已经提到的 r.text.r.status_code……获取文本方式的响应体实例: ...

  3. [转] ajax方法

    1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如 ...

  4. L320 What late-night snacking has to do with sleep

    What late-night snacking has to do with sleep Light-night snacking isn’t just something college stud ...

  5. L304 What Is Death?

    How should we define the death of a person? Philosophers and physicians have long pondered this ques ...

  6. TensorFlow函数:tf.lin_space

    函数:tf.lin_space 别名: tf.lin_space tf.linspace lin_space( start, stop, num, name=None ) 参见指南:生成常量,序列和随 ...

  7. 关于 数据库 my_slq的 安装及其卸载

    安装的时候 注意事项 自定后 根据电脑的系统版本 选择32  或者64  然后选择→方向键 密码默认是123456  或者  123123 查看装的数据库是否安装好了 如何完全卸载 mysql 数据库 ...

  8. libusb示例

    #include <stdio.h> #include <libusb-1.0/libusb.h> #include <stdint.h> #include < ...

  9. python中pass语句的作用是什么

    pass语句不会执行任何操作,一般作为占位符或者创建站位程序,whileFalse:pass.

  10. http状态码301和302的区别

    1.官方的比较简洁的说明: 301 redirect: 301 代表永久性转移(Permanently Moved) 302 redirect: 302 代表暂时性转移(Temporarily Mov ...