What is forward declaration in c++?

lital maatuk picture lital maatuk · Feb 7, 2011 · Viewed 17.1k times · Source

This answer says:

… Finally,

typedef struct { ... } Foo;

declares an anonymous structure and creates a typedef for it. Thus, with this construct, it doesn't have a name in the tag namespace, only a name in the typedef namespace. This means it also can't be forward-declared. If you want to make a forward declaration, you have to give it a name in the tag namespace.

What is forward declaration?

Answer

Fred Larson picture Fred Larson · Feb 7, 2011

Chad has given a pretty good dictionary definition. Forward declarations are often used in C++ to deal with circular relationships. For example:

class B; // Forward declaration

class A
{
    B* b;
};

class B
{
    A* a;
};