当前位置:首页 > 编程开发

explicit的作用

webgou17年前 (2010-03-18)编程开发140
class Test { private: int i; public: explicit Test(int ii){ this->i = ii; } }; void main() { Test t(100); t = 5; } explicit的作用就是不允许隐式转换,代码中如果把他去掉,t = 5处就不会 《ANSI/ISO C++ Professional Programmer's Handbook 》 explicit Constructors A constructor that takes a single argument is, by default, an implicit conversion operator, which converts its argument to an object of its class (see also Chapter 3, "Operator Overloading"). Examine the following concrete example: class string { private: int size; int capacity; char *buff; public: string(); string(int size); // constructor and implicit conversion operator string(const char *); // constructor and implicit conversion operator ~string(); }; Class string has three constructors: a default constructor, a constructor that takes int, and a constructor that constructs a string from const char *. The second constructor is used to create an empty string object with an initial preallocated buffer at the specified size. However, in the case of class string, the automatic conversion is dubious. Converting an int into a string object doesn't make sense, although this is exactly what this constructor does. Consider the following: int main() { string s = "hello"; //OK, convert a C-string into a string object int ns = 0; s = 1; // 1 oops, programmer intended to write ns = 1, } In the expression s= 1;, the programmer simply mistyped the name of the variable ns, typing s instead. Normally, the compiler detects the incompatible types and issues an error message. However, before ruling it out, the compiler first searches for a user-defined conversion that allows this expression; indeed, it finds the constructor that takes int. Consequently, the compiler interprets the expression s= 1; as if the programmer had written s = string(1); You might encounter a similar problem when calling a function that takes a string argument. The following example can either be a cryptic coding style or simply a programmer's typographical error. However, due to the implicit conversion constructor of class string, it will pass unnoticed: int f(string s); int main() { f(1); // without a an explicit constructor, //this call is expanded into: f ( string(1) ); //was that intentional or merely a programmer's typo? } 'In order to avoid such implicit conversions, a constructor that takes one argument needs to be declared explicit: class string { //... public: explicit string(int size); // block implicit conversion string(const char *); //implicit conversion ~string(); }; An explicit constructor does not behave as an implicit conversion operator, which enables the compiler to catch the typographical error this time: int main() { string s = "hello"; //OK, convert a C-string into a string object int ns = 0; s = 1; // compile time error ; this time the compiler catches the typo } Why aren't all constructors automatically declared explicit? Under some conditions, the automatic type conversion is useful and well behaved. A good example of this is the third constructor of string: string(const char *); The implicit type conversion of const char * to a string object enables its users to write the following: string s; s = "Hello"; The compiler implicitly transforms this into string s; //pseudo C++ code: s = string ("Hello"); //create a temporary and assign it to s On the other hand, if you declare this constructor explicit, you have to use explicit type conversion: class string { //... public: explicit string(const char *); }; int main() { string s; s = string("Hello"); //explicit conversion now required return 0; } Extensive amounts of legacy C++ code rely on the implicit conversion of constructors. The C++ Standardization committee was aware of that. In order to not make existing code break, the implicit conversion was retained. However, a new keyword, explicit, was introduced to the language to enable the programmer to block the implicit conversion when it is undesirable. As a rule, a constructor that can be invoked with a single argument needs to be declared explicit. When the implicit type conversion is intentional and well behaved, the constructor can be used as an implicit conversion operator.

扫描二维码推送至手机访问。

版权声明:本文由知了博客发布,如需转载请注明出处。

本文链接:https://www.webgou.info/?id=142

标签: explicitc/c
分享给朋友:

“explicit的作用” 的相关文章

【腾讯开源】behaviac行为树解决方案

 摘要:behaviac是腾讯对行为树(Behavior Tree)的一个实现方案。 …

Mac电脑安装OpenClaw教程

 自2026年1月起,一款名为OpenClaw(网友俗称“小龙虾”)的开源AI智能体在国内外社交媒体爆火,并衍生出收费安装服务。北京、上海、深圳等地的不少企业放出了“OpenClaw开发工程师”等岗位,其要求不限于“熟悉OpenC…

VC++动态链接库(DLL)编程深入浅出(一)

1.概论 先来阐述一下DLL(Dynamic Linkable Library)的概念,你可以简单的把DLL看成一种仓库,它提供给你一些可以直接拿来用的变量、函数或类。在仓库的发展史上经历了“无库-静态链接库-动态链接库”的时代。 静态链接库与动态链接库都是共享代码的…

模态对话框和非模态对话框的区别

模态对话框就是指那种:显示出来就不可以点选位于其下面的对话框的对话框;反之的就是非模态对话框。 两者的区别:一. 非模态对话框的模板必须具有Visible风格(Visible=True),否则对话框将不可见,而模态对话框则无需设置该项风格。在实际编程中更加保险的办法是调用CWnd::ShowWind…

Windows下Nginx的启动、停止等命令

 http://wanganwu.blog.163.com/blog/static/7788722012322111417966/ ...…

文件拖动

在VC中,我们可以不利用打开文件对话框来选择文件,从而对文件进行操作,我们也可以使用拖拽来实现这样的功能。具体如下:1.首先,把一个对话框或者窗体的扩展属性【接受文件】勾选上。2.我们对WM_DROPFILES消息进行处理。在这里我们使用到了一个API函数:DragQueryFile...…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。