static_cast 用法
语法:
static_cast<type-name>(expression)
仅当 type-name 可以隐式转换为 expression 所属的类型,或者 expression 可以隐式转换为 type-name 所属的类型,转换才是合法的。否则,编译器会报错。
可以将有继承关系的派生类对象的地址赋给基类指针。即使基类中没有虚函数也可以使用 static_cast 进行转换。
可以将有继承关系的基类对象的地址赋给派生类指针。因为派生类指针可以隐式转换为基类指针,无需显式类型转换,所以可以用  static_cast 进行另一个方向的转换,即将基类指针转换为派生类指针。但是,这样做有什么意义呢?
同理,因为枚举值可以隐式转换为整型,无需显式类型转换,所以可以用 static_cast 将整型转换为枚举类型。
如果将没有继承关系的对象的地址赋给另一个类的指针,编译器会报错。
请看代码一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<cstdio>
class  Base{
   int  dat;
public :
   explicit  Base( int  val) : dat(val){
     printf ( "%s, this=%p\n" , __func__,  this );
   }
#if 0
   virtual  void  act(){
#else
   void  act(){
#endif
     printf ( "%s, this=%p\n" , __func__,  this );
   }
};
class  Sub :  public  Base{
public :
   explicit  Sub( int  val) : Base(val){
     printf ( "%s, this=%p\n" , __func__,  this );
   }
};
class  Other{
   int  dat;
public :
   explicit  Other( int  val) : dat(val){
     printf ( "%s, this=%p\n" , __func__,  this );
   }
};
int  main(){
   Base obase(1);
   Sub osub(2);
   Base *pbase = NULL;
   if (pbase =  static_cast <Base *>(&osub) ){
     pbase->act();
   }
   
  printf ( "---------------\n" );
   Base *ptr = &osub;
   ptr->act();
  printf ( "---------------\n" );
#if 1
   if (Sub *psub =  static_cast <Sub *>(&obase) ){
     psub->act();
   }
#endif
#if 0
   Other oother(3);
   //error: invalid static_cast from type ‘Other*’ to type ‘Base*’
   if (pbase =  static_cast <Base *>(&oother) ){
     pbase->act();
   }
#endif 
}

测试结果:

frank@userver:~/project/test/cpp/rtti$ ./a.out          

Base, this=0x7fff6d478100

Base, this=0x7fff6d478110

Sub, this=0x7fff6d478110

act, this=0x7fff6d478110

---------------

act, this=0x7fff6d478110

---------------

act, this=0x7fff6d478100

代码二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<cstdio>
int  main(){
   enum  eSource{
     eAuxSource = 0,
     eOpticalSource,
     eBtSource,
     eFcSource,
     eSpotifySource,
     eGcSource
   };
#if 0
   eSource src = eBtSource;
#else
   //eSource src = 3; //error: invalid conversion from ‘int’ to ‘main()::eSource’ [-fpermissive]
   eSource src =  static_cast <eSource>(3.1);
#endif
   printf ( "src: %p, %lu, %d\n" , &src,  sizeof (src), src);
}