设为首页 加入收藏

TOP

5.12.7 Old-Style Casts
2013-10-07 15:23:48 来源: 作者: 【 】 浏览:63
Tags:5.12.7 Old-Style Casts

应该在新写的C++(www.cppentry.com) 代码中杜绝旧式强制类型转换,因为用文本查找工具在代码中无法完全找出这种转换。某些编译器可以对旧式转换给出警告,例如g++的-Wold-style-cast 选项。

Prior to the introduction of named cast operators, an explicit cast was performed by enclosing a type in parentheses:

  1. char *pc = (char*) ip; 

The effect of this cast is the same as using the reinterpret_cast notation. However, the visibility of this cast is considerably less, making it even more difficult to track down the rogue cast.

Standard C++(www.cppentry.com) introduced the named cast operators to make casts more visible and to give the programmer a more finely tuned tool to use when casts are necessary. For example, nonpointer static_casts and const_casts tend to be safer than reinterpret_casts. As a result, the programmer (as well as readers and tools operating on the program) can clearly identify the potential risk level of each explicit cast in code.

Although the old-style cast notation is supported by Standard C++(www.cppentry.com), we recommend it be used only when writing code to be compiled either under the C language or pre-Standard C++(www.cppentry.com).

旧式转型有两种写法,第一种看起来像函数调用,但“函数名”是个类型名;第二种是从C 语言里继承的写法。第一种写法其实是对象构造的语法,Type(val)构造一个Type 类型的匿名对象,以val 为其直接初始化(direct-initialization)的实参。C++(www.cppentry.com) 支持第一种写法,是为了在语法上兼容内置类型与class 类型,这在编写模板代码的时候有用。

The old-style cast notation takes one of the following two forms:

  1. type (expr); // Function-style cast notation  
  2. (type) expr; // C-language-style cast notation 

Depending on the types involved, an old-style cast has the same behavior as a const_cast, a static_cast, or a reinterpret_cast. When used where a static_cast or a const_cast would be legal, an old-style cast does the same conversion as the respective named cast. If neither is legal, then an old-style cast performs a reinterpret_cast. For example, we might rewrite the casts from the previous section less clearly using old-style notation:

  1. int ival; double dval;  
  2. ival += int (dval); // static_cast: converts double to int  
  3. const char* pc_str;  
  4. string_copy((char*)pc_str); // const_cast: casts away const  
  5. int *ip;  
  6. char *pc = (char*)ip; // reinterpret_cast: treats int* as char* 

The old-style cast notation remains supported for backward compatibility with programs written under pre-Standard C++(www.cppentry.com) and to maintain compatibility with the C language.

EXERCISES SECTION 5.12.7

Exercise 5.32: Given the following set of definitions,

  1. char cval; int ival; unsigned int ui;  
  2. float fval; double dval; 

identify the implicit type conversions, if any, taking place:

(a) cval = ’a’ + 3; (b) fval = ui - ival * 1.0;
(c) dval = ui * fval; (d) cval = ival + fval + dval;

Exercise 5.33: Given the following set of definitions,

  1. int ival; double dval;  
  2. const string *ps; char *pc; void *pv; 

rewrite each of the following using a named cast notation:

(a) pv = (void*)ps; (b) ival = int(*pc);
(c) pv = &dval; (d) pc = (char*) pv;

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2.1 Primitive Built-in Types 下一篇5.5 Increment and Decrement Ope..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Sphinx : 高性能SQL (2025-12-24 10:18:11)
·Pandas 性能优化 - (2025-12-24 10:18:08)
·MySQL 索引 - 菜鸟教 (2025-12-24 10:18:06)
·Shell 基本运算符 - (2025-12-24 09:52:56)
·Shell 函数 | 菜鸟教 (2025-12-24 09:52:54)