0
我有以下情形。有两个基本类别:基础1,和Base2和两个派生类:衍生sysCommandExecutor其推导如下:使用dynamic_cast指向其他派生类对象
#include <iostream>
using namespace std;
class Base1 { virtual void dummy() {} };
class Base2 { virtual void dumy() {} };
class Derived: virtual public Base1, public Base2
{ int a; };
class sysCommandExecutor : public Base2
{
public:
int b;
Base1 *ptr;
void func(void);
};
void sysCommandExecutor::func(void)
{
Derived *d;
d = dynamic_cast<Derived *>(ptr);
if (d == NULL)
std::cout << "This is NULL" << std::endl;
else
{
// Call a function of Derived class
}
}
int main() {
try {
sysCommandExecutor * sys = new sysCommandExecutor;
sys->func();
return 0;
}
}
我想打电话给内FUNC“派生”类的此功能,但dynamic_cast的失败。 我无法在sysCommandExecutor类中创建函数,因为这是别人的代码。 如何使sysCommandExecutor类中的ptr指针指向派生类对象?
在此先感谢
什么是ptr的assinged?在你的代码中它是一个未初始化的指针 – Gir 2012-08-12 15:24:08
代码看起来很奇怪。似乎没有正确使用多态性。你有一个派生类指向同一个实例。 – 2012-08-12 15:28:56
http://codepad.org/f65rqtYq'没有错误或程序输出。' – Gir 2012-08-12 15:35:23