首页 > 其他 > 详细

static_cast 用法

时间:2017-06-19 09:45:24      阅读:400      评论:0      收藏:0      [点我收藏+]

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

#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

代码二:

#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=\‘#\‘" /pre>


测试结果:
src: 0x7fffe5204fac, 4, 3

本文出自 “用C++写诗” 博客,谢绝转载!

static_cast 用法

原文:http://frankniefaquan.blog.51cto.com/12613979/1939620

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!