BaseClass.h
class BaseClass
{
...
};
SubClass.h
#include "BaseClass.h"
class SubClass : public BaseClass
{
...
};
MyApp.h
class BaseClass;
class SubClass;
class MyApp
{
SubClass *pObject;
BaseClass *getObject()
{
return pObject;
}
};
I get a compiler error: error C2440: ‘return’ : cannot convert from ‘SubClass *’ to ‘BaseClass *’
Why doesn’t it work, surely you can automatically convert to a base-class without any casting?
In
"MyApp.h", you only have forward declarations of the classes, so it’s not known that one derives from the other. You will need to include the"SubClass.h"before the body ofgetObject(); either include it from"MyApp.h", or move the body ofgetObject()into a source file and include it from there.