Talk:Forward declaration

Page contents not supported in other languages.
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.

Requested move[edit]

"Forward declaration" is the first term covered in the article, and the one with an unambiguous meaning. "Forward reference" (as explained later in the article) may be a synonym of "forward declaration", or it may refer to something else. —Quuxplusone 22:05, 24 June 2007 (UTC)[reply]

This article has been renamed from forward reference to forward declaration as the result of a move request. --Stemonitis 20:14, 30 June 2007 (UTC)[reply]

Why require forward declaration?[edit]

This article does a good job of explaining what forward declaration is, but not why it exists. From reading the forward reference section I can make an educated guess that it enables faster compiling, but the article never explicitly tells me. --Tom Edwards (talk) 11:45, 17 August 2009 (UTC)[reply]