关于多继承和纯虚函数问题
#include"stdafx.h"classInterface{public:Interface(){}~Interface(){}//virtualvoidPrint...
#include "stdafx.h"
class Interface
{
public:
Interface(){}
~Interface(){}
//
virtual void PrintFloat(const float &A_Float) = 0;
};
class Base
{
public:
Base(){}
virtual ~Base(){}
//
void PrintBase(){printf("Base\n");}
virtual void PrintInt(const int &A_Int) = 0;
virtual void PrintAB(const int &A_A,const int &A_B) = 0;
};
class Child : public Base
{
public:
Child(){}
~Child(){}
//
void PrintInt(const int &A_Int){printf("Child PrintInt %d\n",A_Int);}
};
class Grandson : public Child,public Interface
{
public:
Grandson(){}
~Grandson(){}
//
void PrintAB(const int &A_A,const int &A_B){printf("Grandson PrintAB %d %d\n",A_A,A_B);}
void PrintFloat(const float &A_Float){printf("Grandson PrintFloat %f\n",A_Float);}
};
int _tmain(int argc, _TCHAR* argv[])
{
Base * l_pBase = new Grandson();
Interface *l_p = (Interface*)l_pBase;
l_p->PrintFloat(123.1);
delete l_p;
//
printf("===============================");
char l_chr;
scanf_s("%c",&l_chr);
return 0;
}
得不到
l_p->PrintFloat(123.1);
这条语句的输出,何解??? 展开
class Interface
{
public:
Interface(){}
~Interface(){}
//
virtual void PrintFloat(const float &A_Float) = 0;
};
class Base
{
public:
Base(){}
virtual ~Base(){}
//
void PrintBase(){printf("Base\n");}
virtual void PrintInt(const int &A_Int) = 0;
virtual void PrintAB(const int &A_A,const int &A_B) = 0;
};
class Child : public Base
{
public:
Child(){}
~Child(){}
//
void PrintInt(const int &A_Int){printf("Child PrintInt %d\n",A_Int);}
};
class Grandson : public Child,public Interface
{
public:
Grandson(){}
~Grandson(){}
//
void PrintAB(const int &A_A,const int &A_B){printf("Grandson PrintAB %d %d\n",A_A,A_B);}
void PrintFloat(const float &A_Float){printf("Grandson PrintFloat %f\n",A_Float);}
};
int _tmain(int argc, _TCHAR* argv[])
{
Base * l_pBase = new Grandson();
Interface *l_p = (Interface*)l_pBase;
l_p->PrintFloat(123.1);
delete l_p;
//
printf("===============================");
char l_chr;
scanf_s("%c",&l_chr);
return 0;
}
得不到
l_p->PrintFloat(123.1);
这条语句的输出,何解??? 展开
展开全部
#include <iostream>
class Interface
{
public:
Interface(){}
virtual ~Interface(){}
//
virtual void PrintFloat(const float &A_Float) = 0;
};
class Base
{
public:
Base(){}
virtual ~Base(){}
//
void PrintBase(){ printf("Base\n"); }
virtual void PrintInt(const int &A_Int) = 0;
virtual void PrintAB(const int &A_A,const int &A_B) = 0;
};
class Child : public Base
{
public:
Child(){}
~Child(){}
//
void PrintInt(const int &A_Int){printf("Child PrintInt %d\n",A_Int);}
};
class Grandson : public Child,public Interface
{
public:
Grandson(){}
~Grandson(){}
//
void PrintAB(const int &A_A,const int &A_B){printf("Grandson PrintAB %d %d\n",A_A,A_B);}
void PrintFloat(const float &A_Float){printf("Grandson PrintFloat %f\n",A_Float);}
};
int BaseFun(Base* l_pBase)
{
Grandson* l_pGrandson = dynamic_cast<Grandson*>(l_pBase);
if(l_pGrandson)
{
//to do:
l_pGrandson->PrintFloat(123.2);
}
return 0;
}
int main(int argc, char* argv[])
{
//申请一块内存,并且在这块内存中放置一个Grandson的实体
//但是该指针类型只能访问Base类型的接口
Base * l_pBase = new Grandson();
//指针仍然指向Grandson的实体,但是该指针父类型只能访问Interface类型的接口
//虚函数处在一个指针表里面,正确的方法是使用dynamic_cast<>操作符
//古老的C语言的强制转换不适用在含有虚表的类型中
//Interface *l_p = (Interface*)l_pBase;
//l_p->PrintFloat(123.1);
Interface *l_p = dynamic_cast<Interface*>(l_pBase);
if(l_p)
{
l_p->PrintFloat(123.1);
}
//总结:你这是不好的编程习惯
//向下类型转化,应该使用dynamic_cast<>操升埋和作符吵盯,并且判断是否转换成液枝功
//示范例子:
BaseFun(l_pBase);
//此处应该使用l_pBase,避免未完全释放
//delete l_p;
delete l_pBase;
printf("===============================\n");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询