强制类型转换符
来源:互联网 发布:淘宝永恒强袭自由高达 编辑:程序博客网 时间:2024/06/08 00:17
#include<iostream>#include<string>using namespace std;class CAnimal{public:virtual void Speak() = 0;};class CCat : public CAnimal //继承了CAnimal,{public:void CatchMice() { cout << "Cat: I caught a mouse!" << endl; }void Speak() { cout << "Cat: Meow!" << endl; }};class Book{};class CDog : public CAnimal {public:void WagTail() { cout << "Dog: I caught a mouse!" << endl; }void Speak() { cout << "Dog: Bow-Wow!" << endl;}};void DetermineType(CAnimal* pAnimal) //确定类型。{ CDog* pDog = dynamic_cast<CDog *>(pAnimal);//运行时的类型识别,if(pDog){ cout << "The animal is a dog?" << endl; pDog->WagTail();}CCat* pCat = dynamic_cast<CCat *>(pAnimal);if(pCat){ cout << "The animal is a cat?" << endl; pCat->CatchMice();}}int main (){ double dpi = 2.54532; int dp = static_cast<int>(dpi); //静态转换,将dpi的double类型转换成int类型复制给变量dp, int dp2 = (int)dpi; //传统C风格的转换, int dp3 = dpi; // 隐式转换, cout << dp << endl; cout << dp2 << endl; char *ps = "Hello xiao cui "; int *psp = reinterpret_cast<int *>(ps);// reinterpret_cast强制类型转换, CAnimal *pAnimal = new CCat(); CCat *pCat = static_cast<CCat *>(pAnimal); Book *pBook = reinterpret_cast<Book *>(pAnimal); const char *pc_str = "Hi xiao cui?";char *pc = const_cast<char *>(pc_str);//const_cast可以将一个const转化为非const类型, return 0;}
0 0
- 强制类型转换符
- 强制类型转换操作符
- 强制转换类型运算符
- 强制类型转换运算符
- 强制类型转换运算符
- C++ 强制类型转换符
- 强制类型转换运算符
- 强制类型转换运算符
- 关于强制类型转换:强制类型转换
- 类型转换&&类型强制转换
- 类型转换&&类型强制转换
- 标准C++中的强制类型转换符
- 标准c++强制类型转换运算符
- 运算符和强制类型转换
- c++ 强制转换类型运算符
- 运算符和类型强制转换
- C++强制类型转换运算符
- 怎样重载强制类型转换操作符
- 用c语言编写的可以计算+-*/的计算器
- RPC(linux)报错 Cannot register service RPC
- wireshark 过滤条件补充!
- 仿网易新闻客户端——BPNews
- centos6.7搭建lnmp及配置
- 强制类型转换符
- 如何利用Spring Cloud构建起自我修复型分布式系统(1)
- 关于设备号的思考
- Linux常用命令
- 堆和栈的区别
- Codeforces 392C Yet Another Number Sequence 题解&代码
- R制作词云
- 交货单可用性检查,库存+批次可用
- WinDBG常用命令