隐式类型转换与转换操作符operator T
来源:互联网 发布:路由器突然没有网络了 编辑:程序博客网 时间:2024/04/29 13:10
C++ 标准允许隐式类型转换,即对特定的类,在特定条件下,某些参数或变量将隐形转换成类对象 ( 创建临时对象 ) .如果这种转换代价很大 ( 调用类的构造函数 ) ,隐式转换将影响性能。
隐式转换的发生条件:函数调用中,当参数类型不匹配,如果隐式转换后能满足类型匹配条件,编译器将启用类型转换。
控制隐式类型转换的两种途径:
1) 减少函数调用的参数不匹配情况:提供签名 ( 函数参数类型 ) 与常见参数类型的精确匹配的重载函数。
2) 限制编译器的启用隐式转换:使用 explicit 限制的构造函数和具名转换函数。
下面的例子将导致隐式类型转换:
1) 未限制的构造函数:
class Widget { // …
Widget( unsigned int widgetizationFactor );
Widget( const char* name, const Widget* other = 0 );
};
2) 转换操作符 ( 定义成 operator T() ,其中 T 为 C++ 类型 )
class String {
public:
operator const char*(); // 在需要情况下, String 对象可以转成 const char* 指针。
};
上面的定义将使很多愚蠢的表达式通过编译 ( 编译器启用了隐式转换 ) .
Assume s1, s2 are Strings:
int x = s1 - s2; // compiles; undefined behavior
const char* p = s1 - 5; // compiles; undefined behavior
p = s1 + '0'; // compiles; doesn't do what you'd expect
if( s1 == "0" ) { ……} // compiles; doesn't do what you'd expect
合理的解决方案:
By default, write explicit on single-argument constructors:
默认时,为单参数 的构造函数加上 explicit :
class Widget { // …
explicit Widget( unsigned int widgetizationFactor );
explicit Widget( const char* name, const Widget* other = 0 );
};
使用提供的转换具名函数代替转换操作符:
Use named functions that offer conversions instead of conversion operators:
class String { // …
const char* as_char_pointer() const; // in the grand c_str tradition
};
隐式转换的发生条件:函数调用中,当参数类型不匹配,如果隐式转换后能满足类型匹配条件,编译器将启用类型转换。
控制隐式类型转换的两种途径:
1) 减少函数调用的参数不匹配情况:提供签名 ( 函数参数类型 ) 与常见参数类型的精确匹配的重载函数。
2) 限制编译器的启用隐式转换:使用 explicit 限制的构造函数和具名转换函数。
下面的例子将导致隐式类型转换:
1) 未限制的构造函数:
class Widget { // …
Widget( unsigned int widgetizationFactor );
Widget( const char* name, const Widget* other = 0 );
};
2) 转换操作符 ( 定义成 operator T() ,其中 T 为 C++ 类型 )
class String {
public:
operator const char*(); // 在需要情况下, String 对象可以转成 const char* 指针。
};
上面的定义将使很多愚蠢的表达式通过编译 ( 编译器启用了隐式转换 ) .
Assume s1, s2 are Strings:
int x = s1 - s2; // compiles; undefined behavior
const char* p = s1 - 5; // compiles; undefined behavior
p = s1 + '0'; // compiles; doesn't do what you'd expect
if( s1 == "0" ) { ……} // compiles; doesn't do what you'd expect
合理的解决方案:
By default, write explicit on single-argument constructors:
默认时,为单参数 的构造函数加上 explicit :
class Widget { // …
explicit Widget( unsigned int widgetizationFactor );
explicit Widget( const char* name, const Widget* other = 0 );
};
使用提供的转换具名函数代替转换操作符:
Use named functions that offer conversions instead of conversion operators:
class String { // …
const char* as_char_pointer() const; // in the grand c_str tradition
};
- 隐式类型转换与转换操作符operator T
- [C++再学习系列] 隐式类型转换与转换操作符operator T
- C++隐式类型转换 operator T
- Operator运算符重载与Implicit隐式类型转换
- Operator运算符重载与Implicit隐式类型转换
- 重载操作符operator()与用户自定义类型转换
- C++ 类型转换操作与操作符重载 operator type() 与 type operator()
- C++类型转换操作符(cast operator)
- C++类型转换操作符(cast operator)
- 类型转换操作符(casting operator)
- 类型转换操作符operator说明
- 隐式类型转换与转换操作符operatorT
- C++中的operator操作符的用法:重载和隐式类型转换
- C++隐式类型转换 operator
- [转贴] C++类型转换操作符(cast operator)
- C++重载类型转换操作符(type cast operator
- C++类型转换操作符(type conversion operator)
- operator类型转换函数
- System.getProperty()系统参数大全
- U盘简单维修方法
- Tomcat 常用优化和配置
- 妈妈的味道
- HTML编辑器内容代码太长无法显示的问题
- 隐式类型转换与转换操作符operator T
- 人生的十个不要等
- 关于 fork
- JavaScript日期函数大全
- 怎么样才能共享优秀软件
- linux札记
- [VS2005 Tip] 自动生成Property【转】
- MD5 加密原理
- js无缝滚动