今天在《C++ Standard Library》中看到 explicit 的作用,在这里做一下笔记,以备下次再次忘记该关键字的作用。

  By using the keyword explicit, you can prohibit a single argument constructor from defining an automatic type conversion. A typical example of the need for this feature is in a collection class in which you can pass the initial size as constructor argument. For example, you could declare a constructor that has an argument for the initial size of a stack:

  通过使用关键字 explicit,你可以禁止一个参数的构造函数定义一个自动类型转换。对于这个需要的一个典型的例子是下面这样的一个集合类:

   class Stack {
explicit Stack(int size); // create stack with initial size
...
};

  Here, the use of explicit is rather important. Without explicit this constructor would define an automatic type conversion from int to Stack. If this happens, you could assign an int to a Stack:

   Stack s;
...
s = 40; // Oops, creates a new Stack for 40 elements and assigns it to s

  The automatic type conversion would convert the 40 to a stack with 40 elements and then assign it to s. This is probably not what was intended. By declaring the int constructor as explicit, such an assignment results in an error at compile time.

Note that explicit also rules out the initialization with type conversion by using the assignment syntax:


Stack s1(40); // OK
Stack s2 = 40; // ERROR

关键字explicit的更多相关文章

  1. C++关键字 explicit

    C++提供了关键字explicit,可以阻止不应该允许的经过转换构造函数进行的隐式转换的发生.声明为explicit的构造函数不能在隐式转换中使用. C++中, 一个参数的构造函数(或者除了第一个参数 ...

  2. C++ 关键字 explicit, export, mutable

    转自 explicit 如果A类有某个构造函数的单个输入参数,是B类(包括基本数据类型)的对象或引用,则C++的编译器会在需要A类形参的函数调用中,自动调用该构造函数,将B类实参隐式地转换为A类实参. ...

  3. Qt C++中的关键字explicit——防止隐式转换(也就是Java里的装箱),必须写清楚

    最近在复习QT,准备做项目了,QT Creator 默认生成的代码 explicit Dialog(QWidget *parent = 0)中,有这么一个关键字explicit,用来修饰构造函数.以前 ...

  4. C# 显式转换关键字 explicit

    不同于隐式转换,显式转换运算符必须通过转换的方式来调用. 如果转换操作会导致异常或丢失信息,则应将其标记为 explicit. 这可阻止编译器静默调用可能产生意外后果的转换操作. 省略转换将导致编译时 ...

  5. c/c++拷贝构造函数和关键字explicit

    c/c++拷贝构造函数和关键字explicit 关键字explicit 修饰构造方法的关键字,加上了,就告诉编译器,不可以隐式初始化对象:不加就可以隐式初始化对象: 下面的代码是可以正常编译执行的,但 ...

  6. 从Qt谈到C++(一):关键字explicit与隐式类型转换

    转载:果冻虾仁 提出疑问 当我们新建了一个Qt的widgets应用工程时.会自动生成一个框架,包含了几个文件. 其中有个mainwindow.h的头文件.就是你要操纵的UI主界面了.我们看看其中的一段 ...

  7. c++关键字explicit

    关键字explicit,可以阻止不应该允许的经过转换构造函数进行的隐式转换的发生.声明为explicit的构造函数不能在隐式转换中使用. C++中, 一个参数的构造函数(或者除了第一个参数外其余参数都 ...

  8. 关键字explicit的作用(转)

    C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造函数是显示的, 而非隐式的, 跟它相对应的另一个关键字是implicit, 意思是隐藏的,类构造函数默认情况 ...

  9. C#的关键字Explicit 和 Implicit

    一.explicit和implicit explicit 关键字用于声明必须使用强制转换来调用的用户定义的类型转换运算符:implicit 关键字用于声明隐式的用户自定义的类型转换运算符. 总结来说: ...

随机推荐

  1. 使用ol,添加图书销售排行榜

    如果想在网页中展示有前后顺序的信息列表,怎么办呢?如,当当网上的书籍热卖排行榜,如下图所示. 这类信息展示就可以使用<ol>标签来制作有序列表来展示. 语法: <ol> < ...

  2. hdu1690 Bus System(最短路 Dijkstra)

    Problem Description Because of the huge population of China, public transportation is very important ...

  3. Java学习----反复做某件事情

    for循环: public class TestFor{ public static void main(String[] args){ for(int x = 1; x < 3; x++) { ...

  4. (转载)最实用的清除浮动代码 css的文字过长裁剪后面跟着省略号

    css: .clearfloat:after{display:block;clear:both;content:"";visibility:hidden;} .clearfloat ...

  5. [javascript]String添加trim和reverse方法

    function trim() { var start, end; start = 0; end = this.length - 1; while(start <= end && ...

  6. strtolower() strtoupper()等字符串大小写转换函数

    $str = "Mary Had A Little Lamb and She LOVED It So"; string strtolower ( string $str )— 将字 ...

  7. Destoon QQ互联一键登录审核不通过的解决方案

    在QQ互联上申请帐号之后提交了审核, 后台填写APPID和KEY之后自己申请的QQ号可以正常登录,但QQ互联审核的时候一直审核不通过说是“您的网站审核未通过,原因是“点击QQ登录按钮提示登录失败或出现 ...

  8. Django models通过DateTimeField保存到MySQL的时间的时区问题

    最近开始使用Django开发一些系统,在models.py中设置一些数据库表结构并给日期时间字段赋初值,不过在使用的过程中,遇到一点问题.问题是,我本来服务器使用的市区是“Asia/Shanghai” ...

  9. Cmakelist.txt

    在LINUX下面编程需要注意的是,需要自己配置链接库,比如 TARGET_LINK_LIBRARIES(),需要把自己要加的库加进去

  10. Spring ApplicationContext的事件机制

    ApplicationContext的事件机制是观察者设计模式的实现,通过 ApplicationEvent 类和 ApplicationListener 接口,可以实现 ApplicationCon ...