ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++的类型转换

2021-12-20 16:05:59  阅读:225  来源: 互联网

标签:类型转换 cout reinterpret void C++ cast static


  1 类型转换名称和语法

  C 风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:

  TYPE b=(TYPE)a

  C++ 风格的类型转换提供了4 种类型转换操作符来应对不同场合的应用。

  static_cast 静态类型转换。如int转换成charreinterpreter_cast 重新解释类型dynamic_cast 命名上理解是动态类型转换。如子类和父类之间的多态类型转换。const_cast, 字面上理解就是去const属性。 4种类型转换的格式:

  TYPE B=static_cast (a)

  类型转换一般性介绍

  1)static_cast<>() 静态类型转换,编译的时c++编译器会做类型检查;

  基本类型能转换 但是不能转换指针类型

  2)若不同类型之间,进行强制类型转换,用reinterpret_cast<>() 进行重新解释

  3)一般性结论:

  C语言中 能隐式类型转换的,在c++中可用 static_cast<>()进行类型转换。因C++编译器在编译检查一般都能通过;C语言中不能隐式类型转换的,在c++中可以用 reinterpret_cast<>() 进行强行类型 解释。

  总结:static_cast<>()和 reinterpret_cast<>() 基本上把C语言中的 强制类型转换给覆盖reinterpret_cast<>()很难保证移植性。

  4)dynamic_cast<>(),动态类型转换,安全的基类和子类之间转换;运行时类型检查

  5)const_cast<>(),去除变量的只读属性

  static_cast 用法和 reinterpret_cast 用法

  void main01()

  {

  double dPi=3.1415926;

  //1静态的类型转换: 在编译的时 进行基本类型的转换 能替代c风格的类型转换 可以进行一部分检查

  int num1=static_cast (dPi); //c++的新式的类型转换运算符

  int num2=(int)dPi; //c语言的 旧式类型转换

  int num3=dPi; //隐士类型转换

  cout << "num1:" << num1 << " num2:" << num2 << " num3:" << num3 << endl;

  char *p1="hello wangbaoming " ;

  int *p2=NULL;

  p2=(int *)p1;

  //2 基本类型能转换 但是不能转换指针类型

  //p2=static_cast (p1); //“static_cast”: 无法从“char *”转换为“int *”

  //3 可以使用 reinterpret_cast 进行重新解释

  p2=reinterpret_cast (p1);

  cout << "p1 " << p1 << endl;

  cout << "p2 " << p2 << endl;

  //4 一般性的结论: c语言中 能隐式类型转换的 在c++中可以用 static_cast<>()进行类型转换 //C++编译器在编译检查一般都能通过

  //c语言中不能隐式类型转换的,在c++中可以用 reinterpret_cast<>() 进行强行类型 解释

  system("pause");

  return ;

  }

  dynamic_cast 用法和 reinterpret_cast 用法

  class Animal

  {

  public:

  virtual void cry() =0;

  };

  class Dog : public Animal

  {

  public:

  virtual void cry()

  {

  cout << "wangwang " << endl;

  }

  void doSwim()

  {

  cout << "我要狗爬" << endl;

  }

  };

  class Cat : public Animal

  {

  public:

  virtual void cry()

  {

  cout << "miaomiao " << endl;

  }

  void doTree()

  {

  cout << "我要爬树" << endl;

  }

  };

  class Book

  {

  public:

  void printP()

  {

  cout << price << endl;

  }

  private:

  int price;

  };

  void ObjPlay(Animal *base)

  {

  base->cry();

  Dog *pDog=dynamic_cast(base);

  if (pDog !=NULL)

  {

  pDog->cry();

  pDog->doSwim();

  }

  Cat *pCat=dynamic_cast(base);

  if (pCat !=NULL)

  {

  pCat->cry();

  pCat->doTree();

  }

  }

  void main02()

  {

  Animal *base=NULL;

  //1 可以把子类指针赋给 父类指针 但是反过来是不可以的 需要 如下转换

  //pdog=base;

  Dog *pDog=static_cast (base);

  //2 把base转换成其他 非动物相关的 err

  //Book *book=static_cast (base);

  //3 reinterpret_cast //可以强制类型转换

  Book *book2=reinterpret_cast (base);

  //4 dynamic_cast用法

  ObjPlay(new Cat());

  system("pause");

  }

  const_cast用法

  //典型用法 把形参的只读属性去掉

  void Opbuf(const char *p)

  {

  cout << p << endl;

  char *p2=const_cast<char*>(p);

  p2[0]='b';

  cout << p << endl;

  }

  void main()

  {

  const char *p1="11111111111";

  char *p2="22222222";

  char *p3=const_cast(p1);

  char buf[100]="aaaaaaaaaaaa";

  Opbuf(buf);

  //要保证指针所执行的内存空间能修改才行 若不能修改 还是会引起程序异常

  //Opbuf("dddddddddddsssssssssssssss");

  system("pause");

  }

  程序员要清除的知道: 要转的变量,类型转换前是什么类型,类型转换后是什么类型。转换后有什么后果。

  一般情况下,不建议进行类型转换;避免进行类型转换。

标签:类型转换,cout,reinterpret,void,C++,cast,static
来源: https://www.cnblogs.com/linjingyg/p/15711234.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有