File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11## Effective C++
22
3- 1 . 条款1
4- 2 .
3+ 1 . 视 C++ 为一个语言联邦(C、Object-Oriented C++、Template C++、STL)
4+ 2 . 宁可以编译器替换预处理器(尽量以 const、enum、inline 替换 #define)
Original file line number Diff line number Diff line change 1+ // 1. (T)expression C风格的转型
2+ // 2. T(expression) 函数风格的转型
3+ // 2种风格的转型 均称为旧式转型
4+
5+ // C++ 提供4个新式转型
6+ // 1. 用于C++对象的常量转型
7+ // const_cast<T>(expression)
8+ // 2. 主要用于执行安全向下转型
9+ // dynamic_cast<T>(expression)
10+ // 3. 执行低级转型(将一个int指针指向int)
11+ // reinterpret_cast<T>(expression)
12+ // 4. 强迫隐式转换 例如 non-const 2 const; int 2 double
13+ // static_cast<T>(expression)
14+
15+ // class Wight {
16+ // public:
17+ // explicit Wight(int size);
18+ // };
19+
20+ // void doSomeWork(const Wight& w);
21+
22+ // // 将int型转换为wight 函数风格的转型更适合
23+ // void when_use_old_cast() {
24+ // // 1. 函数风格的转型
25+ // doSomeWork(Wight(15));
26+ // // 2. C++风格的转型 static_cast用于隐式转换
27+ // doSomeWork(static_cast<Wight>(15));
28+ // }
29+
30+ #include < iostream>
31+ using std::endl;
32+ using std::cout;
33+
34+ class Window {
35+ public:
36+ virtual void onResize () {
37+ l = 10 ;
38+ }
39+ protected:
40+ int l,w;
41+ };
42+
43+ class SpecialWindow : public Window {
44+ public:
45+ void onResize () {
46+ // 当期望在子类中使用父类的方法时候 只能按照下面的方式调用 这样才能修改子类的成员
47+ // 函数调用会作用在 *this上
48+ Window::onResize ();
49+ // 下面是错误的执行方法 函数只会作用在一个副本上 不会修改子类对象的成员属性
50+ // static_cast<Window>(*this).onResize();
51+ cout << l << endl;
52+ }
53+ };
54+
55+ int main () {
56+ SpecialWindow sw;
57+ sw.onResize ();
58+ }
You can’t perform that action at this time.
0 commit comments