【More Effective C++】条款2:使用C++转型操作符

本文讨论了C++中旧式转型的缺点,如缺乏类型安全和维护困难,然后介绍了static_cast、const_cast、dynamic_cast和reinterpret_cast四种转换操作符,以及它们的用途、场景和注意事项。最后提到了如何用宏定义模拟新式转换以增强类型安全。
摘要由CSDN通过智能技术生成

C旧式转型的缺点包括:

  • 没有类型安全检查:允许将任何指针转换为其他类型指针,可能导致未定义行为的错误;
  • 难以识别和维护:语法(type)expressiontype(expression)在代码中难以被快速识别;

为了解决上述问题,推荐使用C++的四种转型操作符:

static_cast<type>(expression)

用途:用于非多态类型的转换。

场景

  • 基本数据类型之间的转换(如intfloatfloatint)。
  • 将基类指针或引用转换为派生类指针或引用,反之亦然,类型向上转换(派生类到基类);
int firstNumber = 1, secondNumber = 3;
double result = (double)firstNumber/secondNumber; // C
double result2 = static_cast<double>(firstNumber)/secondNumber; // C++

const_cast<type>(expression)

用途:只用于修改类型的constvolatile属性

class Widget {};
class SpecialWidget : public Widget {};
void update(SpecialWidget* psw) {}    

SpecialWidget sw;
const SpecialWidget& csw = sw;
// update(&csw); // error: invalid conversion from ‘const SpecialWidget*’ to ‘SpecialWidget*’
update((SpecialWidget*)(&csw));
update(const_cast<SpecialWidget*>(&csw));
Widget *pw = new SpecialWidget;
// update(pw); // error: invalid conversion from ‘Widget*’ to ‘SpecialWidget*’
update(const_cast<SpecialWidget*>(pw)); // error: invalid const_cast from type ‘Widget*’ to type ‘SpecialWidget*’

 dynamic_cast<type>(expression)

用途:只用于处理多态类型的基类和派生类之间安全向下转换,在运行时检查对象的类型安全性;

注意

  • 当转型对象是指针,转型失败,返回一个nullptr
  • 当转型对象是引用,转型失败,抛出异常
Widget *pw = new SpecialWidget;
updateViaRef(static_cast<SpecialWidget&>(*pw));
//  error: cannot dynamic_cast ‘* pw’ (of type ‘class Widget’) to type ‘class SpecialWidget&’ (source type is not polymorphic)
updateViaRef(dynamic_cast<SpecialWidget&>(*pw)); 

reinterpret_cast<type>(expression)

用途:用于转换”函数指针“类型,不具备移植性

typedef void (*FuncPtr)();
FuncPtr funcPtrArray[10];
int doSomething() { return 0; }

// funcPtrArray[0] = &doSomething; // error: invalid conversion from ‘int (*)()’ to ‘FuncPtr’ {aka ‘void (*)()’} 
funcPtrArray[0] = reinterpret_cast<FuncPtr>(&doSomething);

利用宏定义模拟新式转换

注意:模拟的dynamic_cast不会进行是否转型成功的检查

#define static_cast(TYPE,EXPR) ((TYPE)(EXPR))
#define const_cast(TYPE,EXPR) ((TYPE)(EXPR))
#define reinterpret_cast(TYPE,EXPR) ((TYPE)(EXPR))
#define dynamic_cast(TYPE,EXPR) ((TYPE)(EXPR))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨主任o_o

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值