Another method is to use an intermediate class, plus pointers longer, but it works:
This is the header file (yes, I know, the extension "* .hpp" is not standard):
ForwardClassExample.hpp
class ForwardClass { public: virtual void DoSomething(); }; class ContainerClass { ForwardClass* Item; ContainerClass(); ~ContainerClass(); }; class RealClass: ForwardClass { virtual void DoSomething(); };
This is the body file:
ForwardClassExample.cpp
/* constructor */ ContainerClass::ContainerClass() { // create reference to forwaded class item this.Item = new RealClass(); } /* destructor */ ContainerClass::~ContainerClass() { // deletereference to forwaded class item free this.Item(); } void ForwardClass::DoSomething() { // ... } void RealClass::DoSomething() { // ... }
Note:
I suggest getting used to applying pointers to variables instead of direct fields, it may look more complicated, but in the end it allows you to do more things.
It will also prepare you for the use of "links" in the case when you have to work with other programming languages.
Greetings.
umlcat
source share