我需要澄清调用方法,我有一个基类,我尝试使用派生类对象调用派生类函数。可能吗
请告诉我在这个 senario 中我的代码有什么问题
#include "stdafx.h"
#include <malloc.h>
class derived;
class base
{
public:
base()
{
}
~base()
{
}
void setdptr(derived* dptr)
{
m_dptr = dptr;
dptr->sayHello();
}
virtual void addfunc()
{
printf("base class add");
}
derived *m_dptr;
};
class derived : public base
{
public:
derived()
{
}
~derived ()
{
}
void addfunc()
{
printf("derived class add");
}
void sayHello()
{
printf("Say Hello");
}
void setDeriveToBase()
{
setdptr(this);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
derived dp;
dp.setDeriveToBase();
return 0;
}