consider these c++ fragments:
foo.h:
class foo
{
bar myobj;
};
bar.h:
class bar
{
foo *yourobj;
};
other file:
#include "foo.h" //because foo.h is included first bar will not be defined in foo.h
#include "bar.h"
foo container;
bar blah;
I know I didn't bother to write constructors and all that but you get the idea. Anyone know a way to remedy this situation?
Another way would be to make only one header that declares all your classes.
[classes.h]
[foo.cpp]
[bar.cpp]
There are several common techniques to do this.
First, use forward declarations when you can. Second, if part of the circular dependency depends on functions in a class, make that class inherit from an "interface" class which provides declarations of those functions. Lastly, use PIMPL (pointer to implementation details). Instead of listing all the fields in the class declaration, just include a pointer to the actual class data.
For example in
foo.h
And in
foo.cpp