Question

How do I do forward referencing / declaration in C++ to avoid circular header file references?

I have the #ifndef guard in the header file, yet memory tells me I need this forward referencing thing - which i've used before >< but can't remember how.

Was it helpful?

Solution

You predeclare the class without including it. For example:

//#include "Foo.h" // including Foo.h causes circular reference
class Foo;

class Bar
{
...
};

OTHER TIPS

I believe the correct term for what you are talking about is "forward declaration". "Forward referencing" would be a bit confusing.

You won't get circular header files references if you have #ifndef guards. That's the point.

Forward referencing is used to avoid #include(ing) header files for objects you use only by pointer or reference. However, in this case you are not solving a circular reference problem, you're just practicing good design and decoupling the .h file from details it doesn't need to know.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top