recursive classes references
Hi!
I am making an extension and I have some problems
i have two classes and four files:
cMother.cpp, cMother.h, cSon.cpp and cSon.h...
the classes are CMother and CSon.
Now, the Mother should have
CSon Sons[3];
and the son should have a reference to the mother
CMother *mother;
The Son is created from mother's method and passed "this" which the son sets to its mother.
The problem here is the obvious recursive relation between classes where one should be declared before the other and vice versa.
Is there any way to make this work?
I am more of a C# programmer, so I have some difficulties returning to C++ since C# doesn't care which is declared first...
thank you.
Re: recursive classes references
As the CMother class contains actual instances of the CSon class, CSon must be declared first so that the compiler knows how big CMother is. Because the CSon class only contains a pointer to CMother, just declare it as "class CMother *mother" to let C++ know that "CMother" is a class and not a coding error. All class pointers are the same size, so it doesn't need the actual definition of the CMother class until you try to use the pointer.
In C# all class variables are actually only references to the class, so it doesn't need to know the size of the class, only the size of a reference to a class.
Re: recursive classes references
thanks for quick response.
I knew that there must be a simple solution to this since CSon only contains a pointer to CMother, but didn't know how to make it work.
I tried declaring
class CMother; above CSon but didn't work...
This however works.
Thank you again.