I need clarification in calling methodologies, i have a base class and from which i try to call derived class function using derived class object. is it possible
Pls tell me whats wrong in my code in this 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;
}
In the
setdptrfunction in classbase, you don’t have thederivedclass fulle defined yet so the compiler doesn’t yet know what members are inderived.Move the actual definition of
setdptrto after the declaration ofderivedand it should work.Something like this: