现在的位置: 首页 > 综合 > 正文

父类指针调用子类成员函数的强制转换 方法

2014年01月31日 ⁄ 综合 ⁄ 共 261字 ⁄ 字号 评论关闭

#include "stdafx.h"
#include <conio.h>

class a
{
};

class b : public a
{
public:
void testb(void)
{
printf("\nhello!\n");
}
};

class c 
{
public:
void testc(void)
{
m_p = new a;
((b*)m_p)->testb();//明确告诉编译器,转换类型
}

private:
a* m_p;
};

int _tmain(int argc, _TCHAR* argv[])
{
c cc;
cc.testc();
getch();
return 0;
}

抱歉!评论已关闭.