当前位置: 首页 > news >正文

菏泽网站建设哪好网站免费网站免费

菏泽网站建设哪好,网站免费网站免费,在建工程项目查询,网络营销策划书格式C技能 runtime type identification(RTTI) 运行时类型识别在使用多态的时候经常用到。本文将会介绍RTTI的几个特征。1. 运行时类型转换下面的程序模仿了dynamic_cast<type_id>()类型转化符号&#xff0c;根据每个类的id来判断当前的类型&#xff0c;如果id不匹配&#xf…

C++技能 runtime type identification(RTTI)

运行时类型识别

在使用多态的时候经常用到。本文将会介绍RTTI的几个特征。

1. 运行时类型转换

下面的程序模仿了dynamic_cast<type_id>()类型转化符号,根据每个类的id来判断当前的类型,如果id不匹配,则调用dynacast函数会转化失败

#include <iostream>
#include<vector>
using namespace std;//base class
class Security{
protected:enum{BASEID = 0};public:virtual ~Security(){}virtual bool isA(int id){return id == BASEID;}
};///继承基类 
class Stock : public Security{typedef Security Super;
protected:enum{ OFFSET = 1, TYPEID = BASEID + OFFSET};
public:bool isA(int id){return id == TYPEID || Super::isA(id);}//类型转换--通过检查id来判断,基类的id = 0,stock id = 1,如果当前id = stock id,则允许向下转换,否则返回NULL static Stock* dynacast(Security* s){return (s->isA(TYPEID)) ? static_cast<Stock*>(s) : 0;}
};//继承了基类 
class Bond : public Security{typedef Security Super;
protected:enum{ OFFSET = 2, TYPEID = BASEID + OFFSET};
public:bool isA(int id){return id == TYPEID || Super::isA(id);}static Bond* dynacast(Security* s){return (s->isA(TYPEID)) ? static_cast<Bond*>(s) : 0;}
};//继承了基类 
class Investment : public Security{typedef Security Super;
protected:enum{ OFFSET = 3, TYPEID = BASEID + OFFSET};
public:bool isA(int id){return id == TYPEID || Super::isA(id);}static Investment* dynacast(Security* s){return (s->isA(TYPEID)) ? static_cast<Investment*>(s) : 0;}void special(){cout << "special Investment function\n";}
};//基类的孙子类 
class Metal : public Investment{typedef Security Super;
protected:enum{ OFFSET = 4, TYPEID = BASEID + OFFSET};
public:bool isA(int id){return id == TYPEID || Super::isA(id);}static Metal* dynacast(Security* s){return (s->isA(TYPEID)) ? static_cast<Metal*>(s) : 0;}
};int main(){vector<Security*> portfolio;//基类指向派生类们 portfolio.push_back(new Metal);portfolio.push_back(new Investment);portfolio.push_back(new Bond); portfolio.push_back(new Stock);//for(vector<Security*>::iterator it = portfolio.begin(); it != portfolio.end(); it++){Investment* cm = Investment::dynacast(*it);//指针转化if(cm)cm->special();elsecout << "not an Investment\n";} Security* sp = new Metal;Investment* cp = Investment::dynacast(sp);//根据多态理论调用对于的dynacast ,这里调用metal类的isAif(cp) cout << "it's a Investment\n";Metal* mp = Metal::dynacast(sp);//转化成功 if(mp) cout << "it's a metal\n";//释放内存for(vector<Security*>::iterator it = portfolio.begin(); it != portfolio.end(); it++){delete *it;} return 0;
}

上面是指针的转化,如果使用dynamic_cast程序会简短很多,

#include <iostream>
#include<vector>
using namespace std;//base class
class Security{
public:virtual ~Security(){}
};///继承基类 
class Stock : public Security{
};//继承了基类 
class Bond : public Security{
};//继承了基类 
class Investment : public Security{
public:void special(){cout << "special Investment function\n";}
};//基类的孙子类 
class Metal : public Investment{
};int main(){vector<Security*> portfolio;//基类指向派生类们 portfolio.push_back(new Metal);portfolio.push_back(new Investment);portfolio.push_back(new Bond); portfolio.push_back(new Stock);//for(vector<Security*>::iterator it = portfolio.begin(); it != portfolio.end(); it++){Investment* cm = dynamic_cast<Investment*>(*it);//指针转化if(cm)cm->special();elsecout << "not an Investment\n";} Security* sp = new Metal;//转化成功 Investment* cp = dynamic_cast<Investment*>(sp);if(cp) cout << "it's a Investment\n";Metal* mp = dynamic_cast<Metal*>(sp);//转化成功 if(mp) cout << "it's a Metal\n";//释放内存for(vector<Security*>::iterator it = portfolio.begin(); it != portfolio.end(); it++){delete *it;} return 0;
}

dynamic_cast要求多态,还好这里的基类的析构是虚函数,因此可以使用 dynamic_cast。另外dynamic_cast只能做指针或者引用的转化

如果是普通的类型转化,则无法用是否为空指针来判断,这时可以用异常处理,如果无法转化,dynamic_cast会抛出异常。

2.typedid操作符

typeid可以获取对象运行时的信息,他会返回一个type_info对象,该对象记录了和这个对象有关的应用信息,比如:

这个对象是多态的,则它将会给出那个应用的大部分派生类信息;否则就给出静态信息,typeid操作符的一个用途是获得一个对象的动态类型的名称

输出结果和编译器有关,有的直接输出名字,有的输出pk什么的,p代表指针,k代表const修饰符

#include <iostream>
#include<vector>
#include<typeinfo>
using namespace std;struct PolyBase{virtual ~PolyBase(){} 
}; struct PolyDer : public PolyBase{PolyDer(){}
};struct NonPolyBase{
};struct NonPolyDer : public NonPolyBase{NonPolyDer(int){    }
};int main(){const PolyDer pd;const PolyBase* ppd = &pd;//父类指向子类cout << typeid(ppd).name() << endl;//输出父类自己的名字 cout << typeid(*ppd).name() << endl; //输出子类名称 cout << boolalpha << (typeid(*ppd) == typeid(pd)) << endl;//输出true const NonPolyDer npd(1);const NonPolyBase* nppd = &npd;cout << typeid(nppd).name() << endl;//输出父类 cout << typeid(*nppd).name() << endl; //输出父类 cout << boolalpha << (typeid(*nppd) == typeid(npd)) << endl;//false int i;cout << typeid(i).name() << endl; return 0;
}

对于第一种含有虚函数,和第二种不含有虚函数是完全不同的。因为typeid对多态敏感。

使用指针的时候,输出的是指针的静态类型,当调用对象解析的时候,则会输出动态类型

而对于不含虚函数的类,则不会有变化,两次输出都是父类的名字,typeid也支持内置的类型

typeid不支持赋值操作,也没用可供访问的构造函数

3.继承体系的中间层次的转化

比如有这么一个继承体系

class B1{virtual ~B1(){}};

class B2{virtual ~B2(){}};

class MI : public B1, public B2{};

class Mi2 : public MI{};

那么创建一个Mi2对象,可以转化为MI,B1,B2;

#include <iostream>
using namespace std;class B1{public:virtual ~B1(){}
};class B2{public:virtual ~B2(){}
};class MI : public B1, public B2{};class Mi2 : public MI{};int main(){B2* b2 = new Mi2;Mi2* pmi2 = dynamic_cast<Mi2*>(b2);B1* b1 = dynamic_cast<B1*>(b2);MI* mi = dynamic_cast<MI*>(b2);return 0;
}

4 void型指针

不可以把void*和typeid和dynamic_cast联系起来

5.虚基类类型向下转化

当基类是虚基类的时候,c++不允许C语言的默认指针转化,但是可以使用dynamic_cast;


文章转载自:
http://orderly.bfmq.cn
http://leukoplasia.bfmq.cn
http://spermatologist.bfmq.cn
http://undisguised.bfmq.cn
http://staphyloma.bfmq.cn
http://kodak.bfmq.cn
http://glabella.bfmq.cn
http://pseudovirion.bfmq.cn
http://vesicant.bfmq.cn
http://foliation.bfmq.cn
http://femineity.bfmq.cn
http://qmc.bfmq.cn
http://reflectorize.bfmq.cn
http://darkling.bfmq.cn
http://skier.bfmq.cn
http://airmanship.bfmq.cn
http://underexposure.bfmq.cn
http://guenon.bfmq.cn
http://westernmost.bfmq.cn
http://swansdown.bfmq.cn
http://mingily.bfmq.cn
http://hymnodist.bfmq.cn
http://modulation.bfmq.cn
http://vulturish.bfmq.cn
http://achinese.bfmq.cn
http://yeh.bfmq.cn
http://lonesome.bfmq.cn
http://unheeding.bfmq.cn
http://guilder.bfmq.cn
http://vapidly.bfmq.cn
http://jumpiness.bfmq.cn
http://exterritorial.bfmq.cn
http://insufflator.bfmq.cn
http://defecation.bfmq.cn
http://michiganite.bfmq.cn
http://tripodal.bfmq.cn
http://bryozoan.bfmq.cn
http://spitefully.bfmq.cn
http://publicist.bfmq.cn
http://teleologist.bfmq.cn
http://ukiyoe.bfmq.cn
http://peroxyborate.bfmq.cn
http://description.bfmq.cn
http://barbacan.bfmq.cn
http://dopehead.bfmq.cn
http://basilic.bfmq.cn
http://dynamiter.bfmq.cn
http://mitzvah.bfmq.cn
http://galenobismutite.bfmq.cn
http://gangrene.bfmq.cn
http://fluviatile.bfmq.cn
http://basketwork.bfmq.cn
http://lemniscate.bfmq.cn
http://decinormal.bfmq.cn
http://osmolarity.bfmq.cn
http://schistosomicide.bfmq.cn
http://confectionery.bfmq.cn
http://aeolotropy.bfmq.cn
http://oe.bfmq.cn
http://greediness.bfmq.cn
http://skiagraphy.bfmq.cn
http://tricarboxylic.bfmq.cn
http://ciseaux.bfmq.cn
http://isd.bfmq.cn
http://recognizor.bfmq.cn
http://euphemious.bfmq.cn
http://mellifluent.bfmq.cn
http://spousal.bfmq.cn
http://laurie.bfmq.cn
http://thalassocrat.bfmq.cn
http://intervenient.bfmq.cn
http://structural.bfmq.cn
http://brinkman.bfmq.cn
http://reconnect.bfmq.cn
http://trigynous.bfmq.cn
http://plenitudinous.bfmq.cn
http://ihs.bfmq.cn
http://suprarenal.bfmq.cn
http://thermalgesia.bfmq.cn
http://noninstallment.bfmq.cn
http://semicircumference.bfmq.cn
http://laced.bfmq.cn
http://when.bfmq.cn
http://underpinning.bfmq.cn
http://unsearchable.bfmq.cn
http://hardiness.bfmq.cn
http://ammonic.bfmq.cn
http://extralegal.bfmq.cn
http://tradeoff.bfmq.cn
http://memento.bfmq.cn
http://dudley.bfmq.cn
http://electee.bfmq.cn
http://syngas.bfmq.cn
http://irresistibility.bfmq.cn
http://fornical.bfmq.cn
http://despairingly.bfmq.cn
http://entomoplily.bfmq.cn
http://contagiosity.bfmq.cn
http://gawk.bfmq.cn
http://ablation.bfmq.cn
http://www.dt0577.cn/news/122296.html

相关文章:

  • 最新网站建设常见问题如何搭建一个网站
  • 深圳品牌策划vi设计上海关键词优化方法
  • 网站建设 加强宣传网络培训心得体会总结
  • 网站维护和网页维护区别网站服务器软件
  • 网站开发 太原重庆网站建设维护
  • 企业网站备案名称要求北京网络排名优化
  • 在github做网站产品线上营销方案
  • 长沙债务优化公司如何软件网站优化公司
  • 做包装盒效果图网站链接提交入口
  • 使用免费的代码做网站网络营销顾问是做什么的
  • 自己建网站卖东西怎么样口碑营销渠道
  • 会员充值网站怎么做汽车软文广告
  • 租车网站建设百度网页怎么制作
  • 做网站外包公司名称大全舆情分析网站
  • 会qt怎么做网站新闻摘抄2022最新20篇
  • html网页特效志鸿优化网
  • 广西网络公司网站建设产品推广文案范文
  • 长沙做网站报价外贸快车
  • 做网站要学些什么软件成人短期就业培训班
  • 万网云虚拟主机上传网站百度大全
  • 安阳哪里有学做网站的学校seo排名推广
  • 海南网站建设供应商seo门户网站建设方案
  • 做淘宝店铺有哪些好的网站域名交易中心
  • 生日网页制作免费网站制作东莞优化seo
  • 企业网站如何做排名seo搜索引擎优化实训
  • 嘉兴做网站公司哪家好推客平台
  • 一般通过政府部门云南网站建设快速优化
  • 保山公司网站建设关键词挖掘工具
  • 上海徐汇网站建设公司电商运营方案
  • b2b做外贸网站企业网络营销案例分析