有一阵子没有关注C++了,随着年纪增长记忆力下降,感觉需要一个小抄记录些C++的知识可以随时查阅,碰到什么值得记录知识点就记录在这里,时不时更新下这个文档。
1 Lvalues & Rvalues
- lvalue和rvalue继承自C语言,在C语言中是这样定义的:能在赋值操作左边的是lvalue而rvalue不能
- 但是在C++中则lvalue和rvalue的区别则复杂很多,大致说来就是一个对象被用作右值时,用的是对象的值(内容),一个对象被用作左值时,用的是对象的标识(内存中的位置)
- 对于运算符(赋值,取址等操作)来说,不同之处是运算符需要lvalue还是rvalue,返回的是lvalue还是rvalue;重要的一点就是可以在需要rvalue的地方使用lvalue)在需要lvalue的地方则不能使用rvalue(比如std::move)
- 赋值操作需要lvalue在赋值运算符左边(右边都可以),且产出的是一个左值
- 取址操作需要lvalue,且返回的是一个rvalue的指针,
- 解引用,下标操作(vector、 string等的[]运算符)和迭代器解引用使用lvalue和rvalue都可以,且都返回的是lvalue
- 迭代器自增减需要lvalue,且返回lvalue
参考: C++ Primer 5th Edition 4.1.1 P135
2 Named Cast
Named cast背后的思想就是让程序员显示的表达转型(cast)的意愿1。
2.1 static_cast
static_cast可用于C风格的转型(int to double, double to int, char to int等),const类型到non-const类型的转换,相同层级类指针之间的转型,子类指针向基类指针转型,基类指针向子类指针转型(不安全,必须确认指向的对象是子类对象)。
2.2 const_cast
const_cast用于去除const特性。
2.3 reinterpret_cast
reinterpret_cast用于不安全的指针之间转型和int到指针转型,必须清楚其带来的后果。
2.4 dynamic_cast
dynamic_cast用于运行时具有继承关系的类之间(子类到基类,基类到子类,兄弟类之间)的指针和引用的安全转型,如果失败指针转型返回空,引用转型抛出bad_cast异常。但是G和B家的C++编程规范都是禁止使用dynamic_cast;除非在UT代码中,比如测试工厂模式类创建的对象类型,或者管理对象与它们的mock之间的关系。为什么要禁止呢,主要是容易出错,会造成判断分支过多不易维护,可以用其它机制比如多态来代替。这里贴出G家编程规范对其缺点描述的原文:
Cons
Querying the type of an object at run-time frequently means a design problem. Needing to know the type of an object at runtime is often an indication that the design of your class hierarchy is flawed.
<p>
Undisciplined use of RTTI makes code hard to maintain. It can lead to type-based decision trees or switch statements scattered throughout the code, all of which must be examined when making further changes.
Decision
RTTI has legitimate uses but is prone to abuse, so you must be careful when using it. You may use it freely in unittests, but avoid it when possible in other code. In particular, think twice before using RTTI in new code. If you find yourself needing to write code that behaves differently based on the class of an object, consider one of the following alternatives to querying the type:
- Virtual methods are the preferred way of executing different code paths depending on a specific subclass type. This puts the work within the object itself.
- If the work belongs outside the object and instead in some processing code, consider a double-dispatch solution, such as the Visitor design pattern. This allows a facility outside the object itself to determine the type of class using the built-in type system.
参考
1 TC++PL 11.5.2. Named Casts
2 https://google.github.io/styleguide/cppguide.html#Run-Time_Type_Information__RTTI_
3 https://google.github.io/styleguide/cppguide.html#Casting