explicit关键字的含义和用法

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Sales_item item1,item2,sum; while(cin>>item1>>item2) {
try{ sun=item1+item2; }catch(const isbn_mismatch &e) { cerr<<e.what()<<"left is:"<<e.right<<endl; } } 用于用户自定义类型的构造函数,指定它是默认的构造函数, 不可用于转换构造函 数.因为构造函数有三种:1 拷贝构造函数 2 转换构造函数 3 一般的构造函数(我自己 的术语^_^) 另:如果一个类或结构存在多个构造函数时,explicit 修饰的那个构造函数就是默认 的 class isbn_mismatch:public std::logic_error{ public: explicit isbn_missmatch(const std::string &s):std:logic_error(s){} isbn_mismatch(const std::string &s,const std::string &lhs,const std::string &rhs): std::logic_error(s),left(lhs),right(rhs){} const std::string left,right; virtual ~isbn_mismatch() throw(){} }; isbn is:"<<e.left<<"right isbn
} .... MyClass obj = 10; //err,can't non-explict convert class isbn_mismatch:public std::logic_error{ public: explicit isbn_missmatch(const std::string &s):std:logic_error(s){} isbn_mismatch(const std::string &s,const std::string &lhs,const std::string &rhs): std::logic_error(s),left(lhs),right(rhs){} const std::string left,right; virtual ~isbn_mismatch() throw(){} };
Sales_item& operator+(const Sales_item &lhs,const Sales_item rhs) {
if(!lhs.same_isbn(rhs)) throw isbn_mismatch("isbn missmatch",lhs.book(),rhs.book()); Sales_item ret(lhs); ret+rhs; return ret; }
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, }
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:
Sales_item item1,item2,sum; while(cin>>item1>>item2) { try{ sun=item1+பைடு நூலகம்tem2; }catch(const isbn_mismatch &e) { cerr<<e.what()<<"left is:"<<e.right<<endl; } } isbn is:"<<e.left<<"right isbn
这个 《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:
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
c++中 explicit 关键字的含义和用法 2009-02-27 14:56c++中的 explicit 关键字 用来修饰类的构造函数,表明该构造函数是显式的,既然有"显式"那么必然就有" 隐式",那么什么是显示而什么又是隐式的呢? 如果 c++类的构造函数有一个参数,那么在编译的时候就会有一个缺省的转换操 作:将该构造函数对应数据类型的数据转换为该类对象,如下面所示: class MyClass { public: MyClass( int num ); } .... MyClass obj = 10; //ok,convert int to MyClass 在上面的代码中编译器自动将整型转换为 MyClass 类对象,实际上等同于下面的 操作: MyClass temp(10); MyClass obj = temp; 上面的所有的操作即是所谓的"隐式转换"。 如果要避免这种自动转换的功能,我们该怎么做呢?嘿嘿这就是关键字 explicit 的作用了, 将类的构造函数声明为"显示", 也就是在声明构造函数的时候前面添加 上 explicit 即可,这样就可以防止这种自动的转换操作,如果我们修改上面的 MyClass 类的构造函数为显示的,那么下面的代码就不能够编译通过了,如下所 示: class MyClass { public: explicit MyClass( int num );
Sales_item& operator+(const Sales_item &lhs,const Sales_item rhs) { if(!lhs.same_isbn(rhs)) throw isbn_mismatch("isbn missmatch",lhs.book(),rhs.book()); Sales_item ret(lhs); ret+rhs; return ret; }
相关文档
最新文档