RTTI,C++类型转换操作符
body, table{font-family: 微软雅黑; font-size: 10pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}
|
#include <iostream>
using namespace std;
class A
{
public:
virtual void disp()
{ cout<<"A::disp()"<<endl; }
void printA()
{ cout<<"A::printA()"<<endl; }
};
class B : public A
{
public:
void disp()
{ cout<<"B::disp()"<<endl; }
void printB()
{ cout<<"B::printB()"<<endl; }
};
int main()
{
A *pa = new B;
B *pb = dynamic_cast<B *>(pa); //转换安全
if(pb != NULL)
{ pb->printB(); }
else
{ cout<<"转换不安全,退出"<<endl; }
A *p1 = new A;
B *p2 = dynamic_cast<B *>(p1); //转换不安全
if(p2 != NULL)
{ p2->printB(); }
else
{ cout<<"转换不安全,退出"<<endl; }
return 0;
}
|
#include <iostream>
#include <typeinfo>
#include <string.h>
using namespace std;
class Animal //抽象类
{
public:
virtual void Say()=0; //纯虚函数
};
class Dog:public Animal
{
public:
void Say()
{ cout<<"汪汪"<<endl; }
};
class Cat:public Animal
{
public:
void Say()
{ cout<<"喵喵"<<endl; }
};
void play(Animal *pa)
{
if(typeid(*pa) == typeid(Dog)) //判断当前指针是不是Dog类型
{
Dog * dog = dynamic_cast<Dog*>(pa);
dog->Say();
}
if(typeid(*pa) == typeid(Cat))
{
Cat * cat = dynamic_cast<Cat*>(pa);
cat->Say();
}
}
|
int main()
{
Dog dog;
Animal *a = &dog;
cout<<typeid(double).name()<<endl; //只输出类型第一个字母
cout<<typeid(5).name()<<endl;
cout<<"typeid(a).name() "<<typeid(a).name()<<endl; //指针类型
cout<<"typeid(*a).name() "<<typeid(*a).name()<<endl;
//指针所存的值类型
play(a);
Cat cat;
play(&cat);
}
|
|
cout<<typeid(5).name()<<endl; //输出字符串“int”
cout<<typeid(double).name()<<endl; //输出字符串“double”
|
|
class A{……}; //包含虚函数
class B:pulic A{……}
A* pa=new B;
cout<<typeid(*pa).name()<<endl; //输出结果为B
|
|
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
const int i=5;
const int *pi=&i;
//*pi = 6; //不能更改
cout<<"i= "<<i<<endl;
cout<<"*pi = "<<*pi<<endl;
cout<<"pi = "<<pi<<endl;
int *pi1=const_cast<int *>(pi); //为什么这样?因为const int *不能初始化int *
//去除pi的const属性,返回新类型变量,不会影响i
*pi1 = 6;
//*pi = 6; //不能更改
cout<<"i= "<<i<<endl;
cout<<"*pi = "<<*pi<<endl;
cout<<"pi = "<<pi<<endl;
cout<<"*pi1 = "<<*pi1<<endl;
cout<<"pi1 = "<<pi1<<endl;
*(const_cast<int *>(pi))=6; //前面操作可合并为这个
return 0;
}
|
#include <iostream>
using namespace std;
int main()
{
double d = 2.5;
double *pd = &d;
int *pi = reinterpret_cast<int *>(pd);
//int *pi1 = static_cast<int *>(pd); //error
//int *pi2 = dynamic_cast<int *>(pd); //error
int *pi3 = (int *)(pd);
cout<<"pd= "<<pd<<"\t\tpi= "<<pi<<"\t\tpi3= "<<pi3<<endl; //因为指针类型不同,所以值不同
cout<<"*pi= "<<*pi<<"\t\t*pi3= "<<*pi3<<endl;
int a = 3; //这里必须int
int *a1 = reinterpret_cast<int *>(a); //整形量转换为指针,只能值int类型
double *a2 = reinterpret_cast<double *>(a);
cout<<"a1= "<<a1<<"\t\ta2= "<<a2<<endl;
cout<<"*a1= "<<*a1<<"\t\t*a2= "<<*a2<<endl; //段错误
return 0;
}
|
|
#include<iostream>
using namespace std;
class point
{
public:
point(int x=0,int y=0):_x(x),_y(y){}
virtual void show()
{
cout<<"("<<_x<<","<<_y;
}
private:
int _x;
int _y;
};
class point3D:public point
{
public:
point3D(int _x=0,int _y=0,int z=0):point(_x,_y),_z(z){}
virtual void show()
{
point::show();
cout<<","<<_z<<")"<<endl;
}
private:
int _z;
};
class String
{
public:
String():_mchar(new char[1]){}
void show()
{
cout<<_mchar<<endl;
}
private:
char* _mchar;
};
|
int main()
{
//下面的转换本来是无意义和非法的,以后使用ps->Show()
//成员函数时可能会引起内存错误或得到错误的值, 但编译却不出错. 留下隐患
point3D p1(1,2,3);
String* sp = (String*)&p1;
sp->show(); // 编译通过,调用直接段错误 // 派生类里面加了point::限定就不会段错误了
//但改成下面使用static_cast形式进行转换, 在编译时就报错, 能及时发现错误
//sp = static_cast<String*> (&p1); // 编译报错
cout<<"----------------------------------------------------------------"<<endl;
//而下面这种转换之所以能编译通过,是因为CPoint和CPoint3D的指针本来就可以相互转换
point* pBase = static_cast<point*> (&p1);
pBase->show();
cout<<endl;
return 0;
}
|
RTTI,C++类型转换操作符的更多相关文章
- C++强制类型转换操作符 dynamic_cast
dynamic_cast是四个强制类型转换操作符中最特殊的一个,它支持运行时识别指针或引用. >>>>>>>>>>>编译器的RTTI设 ...
- C++强制类型转换操作符 const_cast
const_cast也是一个强制类型转换操作符.<C++ Primer>中是这样描述它的: 1.将转换掉表达式的const性质. 2.只有使用const_cast才能将const性质性质转 ...
- C++强制类型转换操作符 static_cast
static_cast是一个强制类型转换操作符.强制类型转换,也称为显式转换,C++中强制类型转换操作符有static_cast.dynamic_cast.const_cast.reinterpert ...
- 类型转换操作符static_cast、const_cast、dynamic_cast、reinterpret_cast
一.static_cast 对于类型转换,我们常常这么做: (type) expression 引进了static_cast类型转换操作符后,我们只需这样做: static_cast<type& ...
- ECMAScript1.1 js书写位置 | 声明变量 | 基本数据类型 | 数据类型转换 | 操作符 | 布尔类型的隐式转换
js书写位置 由于在写css样式时使用的时双引号,所以我们在写js代码时建议使用单引号(‘’)! 行内式 <input type="button" value="点 ...
- C++的四种cast操作符的区别--类型转换(转)
转自: http://welfare.cnblogs.com/articles/336091.html Q:什么是C风格转换?什么是static_cast, dynamic_cast 以及 r ...
- C++库研究笔记——操作符重载实现类型转换&这样做的意义
目标: 已知这个接口: std::vector<double> add_vec(double *d1, double *d2) { ..... return result; } 我们自定义 ...
- C++的四种cast操作符的区别--类型转换
Q:什么是C风格转换?什么是static_cast, dynamic_cast 以及 reinterpret_cast?区别是什么?为什么要注意? A:转换的含义是通过改变一个变量的类型为别的类型从而 ...
- C++ 类型转换操作与操作符重载 operator type() 与 type operator()
类型转换操作符(type conversion operator)是一种特殊的类成员函数,它定义将类类型值转变为其他类型值的转换.转换操作符在类定义体内声明,在保留字 operator 之后跟着转换的 ...
随机推荐
- Unity--- 资源路径问题
使用 System.IO.Path 这个API得到的路径,其实也是以"\"分隔路径的. 我们在windows下打开资源管理器,某个目录或文件的路径为:E:\uniuProject5 ...
- servlet容器、IOC容器、SpirngMVC
servlet容器(这里指tomcat插件)存放servlet对象,而SpringMVC框架整个是一个servlet对象,而IOC容器 在Boot框架中,会存放产生servlet容器的工厂,工厂依据主 ...
- spring cloud: Hystrix(七):Hystrix的断容器监控dashboard
Hystrix的断容器监控dashboard. dashboard是用来监控Hystrix的断容器监控的,图形化dashboard是如何实现指标的收集展示的. dashboard 本地端口8730 项 ...
- Bash Shell 注释多行的几种方法(转)
很实用的小技巧. 我们shell脚本写好了,但是想一行一行测试,怎么办. 笨方法:每行前面加一个 #,有时候我们原脚本里面本来就有注释,所以想再恢复的时候就麻烦了. Bash Shell 注释多行的几 ...
- Linq to XML 增删改查
Linq to XML同样是对原C#访问XML文件的方法的封装,简化了用xpath进行xml的查询以及增加,修改,删除xml元素的操作.C#访问XML文件的常用类:XmlDocument,XmlEle ...
- 20171023xlVBA递归统计WORD字数
Dim dFilePath As Object, OneKey Sub main_proc() Dim Wb As Workbook, Sht As Worksheet, Rng As Range S ...
- 基于windows使用fabric将gitlab的文件远程同步到服务器(本地)
# -*- coding: utf-8 -*- from fabric.api import env, run, local, put from fabric.operations import su ...
- 文件上传MultipartBody使用方法
最近有使用一个文件上传的功能,需要在请求中添加文件,一起传给服务器 Okhttp提供了这个文件添加然后上传的功能 下面给出核心的代码,然后分析一下 //多个文件上传,Filelist private ...
- 大div中嵌套小div,点击大div时隐藏,点击小div不隐藏
给小div添加一个click事件 <div onClick="event.cancelBubble = true"> //小div
- Vue-router中的导航钩子
vue-router中的导航钩子,主要用来作用是拦截导航,让他完成跳转或取消.(路由守卫) 原文指路:https://blog.csdn.net/weixin_41399785/article/det ...