I have a situation where I have an instanced class that needs to call a function from the enclosing class. The top class is generated code and compiled after the bottom class is. A consequence is the top class name is not known by the bottom class.
class topClass
{
public:
void topFunction();
bottomCLass * bcInst;
}
class bottomClass
{
void * owner;
void someFunction() {owner->topFunction(); }
}
Obviously this won’t work since there’s no definition for topClass.
How can I arrange this so the topClass function can be called from the bottomClass function? I tried using a parent class with a pure virtual function but this crashes when the function is called.
//This is defined and compiled with bottomClass
class classTemplate
{
public:
virtual void topFunction()=0;
}
class topClass : public classTemplate
{
public:
void topFunction();
bottomClass * bcInst;
}
class bottomClass
{
classTemplate * owner;
void someFunction() {owner->topFunction();//Crashes here }
}
Is there a better way to solve this problem? The one thing I can’t do is give bottom class the name/definition of top class, however the presence and name of topFunction is guaranteed.
Move
someFunction()implementation toCPPand include both headers in it. It will help you a lot:topClass.h:
bottomClass.h:
classes.cpp: