dynamic_cast与typeid关键字
2015-10-06 18:06
295 查看
C++的RTTI包含三部分内容:dynamic_cast, typeid和type_info(很少直接使用,但是需要了解一下,type_info的定义是由编译器定的,但是共同的是它包含一个name()函数)
dynamic_cast关键字的用法:
class Base1 {};
class Derive1: public Base1 {};
class Base2
{
public:
virtual void fun(void) {}
};
class Derive2: public Base2 {};
class Derive22:public Base2 {};
void main()
{
cout << typeid(1.1f).name() << endl;
Derive1 d1;
Base1 &b1 = d1;
cout << typeid(b1).name() << endl; //输出“class Base1”,因为Derive1和Base1之间没有多态性
Derive2 d2;
Base2 &b2 = d2;
cout << typeid(b2).name() << endl; //输出“class Derive2”,因为Derive1和Base1之间有了多态性
// 指针强制转化失败后可以比较指针是否为零,而引用却没办法,所以引用制转化失败后抛出异常
Derive2 *pb1 = dynamic_cast<Derive2 *>(&b2); // 输出"true",因为b2本身就确实是Derive2
cout << boolalpha << (0!=pb1) << endl;
Derive22 *pb2 = dynamic_cast<Derive22*>(&b2); //输出"false",因为b2本身不是Derive2
cout << boolalpha << (0 != pb2) << endl;
//引用转化
try
{
Derive2 &rb1 = dynamic_cast<Derive2&>(b2);//输出“true”,因为b2本身就是Derive2类型
cout << "true" << endl;
}
catch (bad_cast) //引用转化时,如果失败返回的是 bad_cast,指针转化失败返回的是 NULL(0),这是二者的不同之处
{
cout << "false" << endl;
}
try
{
Derive22 &rb2 = dynamic_cast<Derive22&>(b2); //b2本身是Derive2类型,不是Derive22类型
cout << "true" << endl;
}
catch (bad_cast)
{
cout << "false" << endl;
}
}
typeid关键字的用法:
class A
{
public:
virtual void func() {}
};
class B:public A
{
public:
void func() {}
};
void main()
{
A *pa;
B b, *pb;
pb = &b;
pa = pb;
cout << "Name1: " <<(typeid(pa).name()) << endl; //输出 class A
cout << "Name2: " <<(typeid(pb).name()) << endl; //输出 class B
cout << "*pa == *pb : " << (typeid(*pa).name() == typeid(*pb).name()) << endl; //二者相等,因为A中有虚函数
}
总结:
typeid(pa)返回的是指针类型。如果要返回对象的类型需要使用引用或者对象作为参数传递给typeid。
dynamic_cast是个运行时操作,但是它完全依赖虚函数表,如果某个两个类之间有继承关系,但是没有虚函数,那么dynamic_cast不能进行转化
dynamic_cast关键字的用法:
class Base1 {};
class Derive1: public Base1 {};
class Base2
{
public:
virtual void fun(void) {}
};
class Derive2: public Base2 {};
class Derive22:public Base2 {};
void main()
{
cout << typeid(1.1f).name() << endl;
Derive1 d1;
Base1 &b1 = d1;
cout << typeid(b1).name() << endl; //输出“class Base1”,因为Derive1和Base1之间没有多态性
Derive2 d2;
Base2 &b2 = d2;
cout << typeid(b2).name() << endl; //输出“class Derive2”,因为Derive1和Base1之间有了多态性
// 指针强制转化失败后可以比较指针是否为零,而引用却没办法,所以引用制转化失败后抛出异常
Derive2 *pb1 = dynamic_cast<Derive2 *>(&b2); // 输出"true",因为b2本身就确实是Derive2
cout << boolalpha << (0!=pb1) << endl;
Derive22 *pb2 = dynamic_cast<Derive22*>(&b2); //输出"false",因为b2本身不是Derive2
cout << boolalpha << (0 != pb2) << endl;
//引用转化
try
{
Derive2 &rb1 = dynamic_cast<Derive2&>(b2);//输出“true”,因为b2本身就是Derive2类型
cout << "true" << endl;
}
catch (bad_cast) //引用转化时,如果失败返回的是 bad_cast,指针转化失败返回的是 NULL(0),这是二者的不同之处
{
cout << "false" << endl;
}
try
{
Derive22 &rb2 = dynamic_cast<Derive22&>(b2); //b2本身是Derive2类型,不是Derive22类型
cout << "true" << endl;
}
catch (bad_cast)
{
cout << "false" << endl;
}
}
typeid关键字的用法:
class A
{
public:
virtual void func() {}
};
class B:public A
{
public:
void func() {}
};
void main()
{
A *pa;
B b, *pb;
pb = &b;
pa = pb;
cout << "Name1: " <<(typeid(pa).name()) << endl; //输出 class A
cout << "Name2: " <<(typeid(pb).name()) << endl; //输出 class B
cout << "*pa == *pb : " << (typeid(*pa).name() == typeid(*pb).name()) << endl; //二者相等,因为A中有虚函数
}
总结:
typeid(pa)返回的是指针类型。如果要返回对象的类型需要使用引用或者对象作为参数传递给typeid。
dynamic_cast是个运行时操作,但是它完全依赖虚函数表,如果某个两个类之间有继承关系,但是没有虚函数,那么dynamic_cast不能进行转化
相关文章推荐
- 主从服务器同步
- foj 犯罪嫌疑人 (脑洞题_)
- 百度2016研发工程师在线编程题
- iOS:多线程同步加锁的简单介绍
- hdu 5428 The Factor 找质因子
- 关于@RequestBody 不能json to pojo
- 堆区、栈区和静态存储区详解
- 大龄屌丝自学笔记--Java零基础到菜鸟--024
- A protocol error occurred. Change of username or service not allowed
- MFC打开一个文件方法汇总
- 遇到1045-Access denied for user 'root'@'localhost' 的问题
- 位运算符 -- 练习篇
- POJ 2001 Shortest Prefixes(字典树)
- oc MRC初步认识
- Properties类(一)
- gethostbyname() -- 用域名或主机名获取IP地址
- 位运算符
- java 8函数式编程与Lambda表达式
- [经济学原理|微观部分]市场失灵与政府调节
- 黑马程序员——OC语言基础---id类型及应用场景