码迷,mamicode.com
首页 > 编程语言 > 详细

c++引入额外开销的情况

时间:2015-07-30 21:30:47      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

技术分享技术分享技术分享技术分享技术分享

#include<iostream>
#include<typeinfo>
using namespace std;
class base
{
public:
virtual void funcA()
{
cout << "base" << endl;
}
};
class derived :public base
{
public:
virtual void funcB()
{
cout << "derived" << endl;
}
};
void funcC(base* p)
{
derived *dp = dynamic_cast<derived *>(p);
if (dp != NULL)
{
dp->funcB();
}
else
{
p->funcA();
}
}
//funcD用typeid操作符
void funcD(base *p)
{
derived* dp = NULL;
if (typeid(*p) == typeid(derived))
{
dp = static_cast<derived*>(p);
dp->funcB();
}
else
p->funcA();
}
int main()
{
base *cp = new derived;
cout << typeid(cp).name() << endl;
cout << typeid(*cp).name() << endl;
funcD(cp);
funcC(cp);
base *dp = new base;
funcC(dp);
funcD(dp);
system("pause");
return 0;
}

运行结果为:

class  base *

class derived

derived

derived

base

base

技术分享


版权声明:本文为博主原创文章,未经博主允许不得转载。

c++引入额外开销的情况

标签:

原文地址:http://blog.csdn.net/wangfengfan1/article/details/47154883

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!