explicit constructor(显示构造函数)
按照默认规定,只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应的数据类型的数据转换为该类对象,如下所示:
- class String
- {
- String(const char* p) //用C风格的字符串p作为初始值
- //........
- }
- String s1 = "hello"; //OK,隐式转换,等价于String s1 = String('hello')
但是有的时候可能会不需要这种隐式转换,如下:
- class String
- {
- String(int n) //本意是预先分配n个字节给字符串
- String(const char* p); //用C风格的字符串p作为初始值
- //........
- }
下面的两种写法是正常的:
- String s2(10); //OK,即分配10个字节的空字符串
- String s3 = String (10); //OK,即分配10个字节的空字符串
但是以下的两种写法会使我们感到疑惑:
- String s4 = 10; //编译通过,即分配了10个字节的空字符串
- String s5 = 'a'; //编译通过,分配int('a')个字节的空字符串
s4和s5分别是把一个int型和char型,隐式转换成了分配若干个字节的空字符串,容易令人误解。
为了避免这种错误的发生,我们可以声明显示的转换,即使用explicit关键字:
- class String
- {
- <span style="color:#cc0000;">explicit</span> String(int n) //本意是预先分配n个字节给字符串
- String(const char* p); //用C风格的字符串p作为初始值
- //........
- }
加上explicit,就抑制了String(int n)的隐式转换
即下面的两种写法仍然是正确的:
- String s2(10); //OK,即分配10个字节的空字符串
- String s3 = String (10); //OK,即分配10个字节的空字符串
但是先前的两种写法就编译不能通过了,例如:
- String s4 = 10; //编译不通过,不允许隐式转换
- String s5 = 'a'; //编译不通过,不允许隐式转换
因此某些时候,explicit可以有效地防止构造函数的隐式转换带来的错误或者误解。
举例进一步阐释:
explicit只对构造函数起作用,用来抑制隐式转换,如下:
- class A
- {
- A(int a);
- };
- int Function(A a);
当调用Function(2)的时候,会隐式转换为A类型。这种情况常常不是我们所想要的结果,所以,要避免这种情形,我们就可以这样写:
- class A
- {
- explicit A(int a);
- };
- int Function(A a);
这样,当调用Function(2)的时候,编译器会给出错误的信息(除非Function有个以int为参数的重载形式),这就避免了在我们毫不知情的情况下出现的错误。
explicit constructor(显示构造函数)的更多相关文章
- 错误: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 ...
- C++ explicit constructor/copy constructor note
C++:explict 作用显示声明构造函数只能被显示调用从而阻止编译器的隐式转换,类似只能用()显示调用,而不能=或者隐式调用 #include <iostream> #include ...
- 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 c ...
- Eclipse错误:Implicit super constructor ClassName is undefined for default constructor. Must define an explicit constructor
public class Test01 { private String name; private int age; public Test01(String name){ this.name = ...
- C++ essentials 之 explicit constructor
这篇博客的源起是我下面的一段代码 #include <bits/stdc++.h> using namespace std; int main(){ priority_queue<l ...
- C# 转换运算符:implicit(隐式),explicit(显示)
//A类 class Cls1 { public string name; //构造函数 public Cls1(string name) { this.name = name; } //implic ...
- 构造器(Constructor)--构造函数
构造器是类型的成员之一,其他成员比如,成员字段,成员函数.狭义上,构造器指的是实例构造器(instance constructor) class Student { public int ID; pu ...
- c#自定义类型的转换方式operator,以及implicit(隐式)和explicit (显示)声明
https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/explicit https://docs.mic ...
- Implicit super constructor Array() is undefined for default constructor. Must define an explicit constructor
因为你的父类已经创建了一个带参的构造函数并且父类中没有无参的构造函数,此时编译器不会为你调用默认的构造函数, 所以子类在继承父类的时候需要在自己的构造函数中显式的调用父类的构造函数,这样才能确保子类在 ...
随机推荐
- QGraphics
QGraphicsView和QGraphicsScene QGraphicsScene提供一个场景,可以添加各种item,QGraphicsView用于将元素显示,并支持旋转和缩放:可以将QGraph ...
- VS调试Libevent流程
下载源码包: libevent--stable.tar.gz 第一:编译libevent 进入VS2010命令提示,切换到libevent的所在目录 nmake /f Makefile.nmake 编 ...
- eclipse git 整合
最近朋友都推荐使用github管理自己的项目,而且免费用户可以有5个仓库,恰好我也想了解下git,借此机会学习一下.github官方指南使用独立第三方git工具来进行版本控制,并不借助于eclipse ...
- MySQL 操作表命令
新建表: create table Itemcats (id int(11) not null auto_increment, primary key (id)) engine=MyISAM auto ...
- vs2008 wince 通过字符串对控件操作
例如:我们知道控件名为"textbox1"需要对textbox1进行赋值:通常我们只要textbox1.text = "你好";即可 此处我们是通过" ...
- POJ 1988
#include<iostream> #include<stdio.h> #include<algorithm> #define MAXN 30005 using ...
- $q -- AngularJS中的服务
此 承诺/延迟(promise/deferred)实现 的灵感来自于 Kris Kowal's QCommonJS Promise建议文档 将承诺(promise) 作为和 异步执行操作(action ...
- Webpack教程一
比较 如果你熟悉原来一系列的构建工具,grunt或者gulp之类的,这里有一篇webpack和他们比较的文章可以读一读. Webpack Compared 安装 先装好node和npm,因为webpa ...
- android模拟器(genymotion)+appium+python 框架执行过程中问题解答
1.case运行过程中中文输入不进去? 答:注意事项 1)需要修改系统编码为utf-8,才能解决中文输入问题,case执行入口文件添加代码如下: import sys reload(sys) sys. ...
- Linux静态库和动态库
Linux 工具 ❑ GCC: The GNU Compiler Collection, containing the GNU C compiler❑ G++: A C++ compiler, inc ...