Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward Referencing or Declaration in C++

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.

like image 701
CVertex Avatar asked Dec 09 '22 22:12

CVertex


2 Answers

You predeclare the class without including it. For example:

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

class Bar
{
...
};
like image 58
antik Avatar answered Dec 21 '22 23:12

antik


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

like image 29
Dima Avatar answered Dec 21 '22 22:12

Dima