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

explicit的作用

webgou17年前 (2010-03-18)编程开发213
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的作用” 的相关文章

iOS的多核编程和内存管理

这次的文章集中与iOS的多核编程和内存管理,为什么?因为iPad 2已经是双核CPU了!虽然iPad 1的应用已经不慢了,但大家完全可以使用苹果的多核编程框架来写出更加responsive的应用。多核运算在iOS中concurrency编程的框架就是GCD(Grand Central Dispatc…

Cortex-A5 MPCore处理器(ARM)

 ARM发布了ARM® Cortex-A5 MPCore处理器,该处理器是目前能够在最多类型的设备上提供互联网功能的、最小、功耗最低的ARM多核处理器。这些设备包括从超低成本手机、多功能手 机和智能移动设备到普遍使用的嵌入式、消费和工业设备。Cortex-A5处理器可以以具有极其…

GDB调试精粹及使用实例

一:列文件清单 1. List (gdb) list line1,line2 二:执行程序 要想运行准备调试的程序,可使用run命令,在它后面可以跟随发给该程序的任何参数,包括标准输入和标准输出说明符()和外壳通配符(*、?、[、])在内。 如果你使用不带参数的run命令,gdb就再次使用你给予前一…

Android核心模块内容概述

Android作为一个移动设备的平台,其软件层次结构包括了一个操作系统(OS),中间件(MiddleWare)和应用程序(Application)。根据Android的软件框图,其Android核心模块结构自下而上分为以下几个层次:第一、操作系统层(OS)第二、各种库(Libraries)和Andr…

[Unity3d]Player Settings导出设置

 unity 导出各个平台设置很方便,最近导出ios时显示状态栏,Status Bar 设置为hide就可以。 可以参考:播放器设置 Player Settings...…

递归实现阶乘

[CODE_LITE] int multi(int n) { if(n == 0) return 1; return (n*multi(n-1)); } int _tmain(int argc, _TCHAR* argv[]) { int sum = multi(4); } [/CODE…

发表评论

访客

看不清,换一张

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