Talk:Forward reference
From Wikipedia, the free encyclopedia
That may be a good example of a "forward reference" but it's not any kind of example of a forward declaration (a topic for which the article redirects to here).
At least in C/C++, forward declaration is about declaring a symbol's type prior to specifying the full definition of that symbol, so that that symbol can be used in contexts that need only the type information and not the full definition.
A C++ example of a header file that uses forward declaration:
#ifndef SYSTEM_H #define SYSTEM_H class Gizmo; class Widget; class System { public: const Gizmo* GetGizmo() const; const Widget* GetWidget() const; private: const Gizmo* m_Gizmo; const Widget* m_Widget; }; #endif
The point of doing this is that this header doesn't depend at all on the definitions of Gizmo and Widget. If this class directly contained a Gizmo (rather than a Gizmo pointer) then this header would need to include the header that defines a gizmo:
#include "gizmo.h"
Avoiding that header-in-header include means that fewer files will have to be recompiled when "gizmo.h" changes.