12

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?

Community
  • 1
  • 1
lital maatuk
  • 5,345
  • 17
  • 53
  • 74
  • You cannot forward declare an *anonymous* struct. – John Feb 07 '11 at 20:32
  • @FredN I'm really not sure what you're pointing at. (This is not helped by the __"this answer"__ link linking to the __question__, so we don't know which answer was meant.) In C++, you can forward-declare a class, but not a typedef name. A "forward declaration" of a class is a class declaration. – sbi Feb 07 '11 at 21:20
  • @sbi: While you're right that the OP was vague (and new to the site so didn't know how to correctly link to a specific answer), there are only two answers on that question that say you cannot forward declare a typedef, and someone looking at either of those answers (I've included the more complete quote from one answer above) will have the context needed to really answer this. – Fred Nurk Feb 07 '11 at 21:52
  • 1
    Please note that this question is tagged **C++** but the quote refers to **C** – MSalters Feb 08 '11 at 08:22

5 Answers5

18

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;
};
Fred Larson
  • 56,061
  • 15
  • 106
  • 157
  • 1
    It's worth noting that you can only declare `B` as a pointer or a reference type because it's what is called _incomplete type_. –  Jun 21 '17 at 12:32
7

"In computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, or a function) for which the programmer has not yet given a complete definition."

-Wikipedia

Chad La Guardia
  • 4,758
  • 3
  • 22
  • 35
  • I read the linked post. And I think this answers the question (in both the title and the post) "What is forward declaration?" Anonymous structs are moot to the question, albeit the OP is incorrect in saying that typedefs cannot be forward declared. – Chad La Guardia Feb 07 '11 at 21:05
  • 4
    Tautologies are not the answer: http://xkcd.com/703/ – allyourcode Jun 20 '11 at 02:03
4

To the best of my knowledge, in C++ the term "forward declaration" is a misnomer. It's simply a declaration under a fancy name.

Community
  • 1
  • 1
sbi
  • 204,536
  • 44
  • 236
  • 426
  • Thank you for clarifying that this is NOT a special type of declaration. It's very easy to think that it is from the way people discuss it! – allyourcode Jun 20 '11 at 02:06
  • On the other hand, why did people see the need to create a special term for a concept that isn't so special? – allyourcode Jun 20 '11 at 02:22
  • 1
    @allyourcode: TTBOMK Stroustrup introduced that name decades before the standard wording was crafted I quoted in the answer I linked to. I can only assume that the name was chosen based on what that declaration was used for, while nobody thought about the bigger picture. Remember that C++ evolved according to concrete needs while those using it limbed along. (Yes, everything was added according to the principle of not adding features to support a single use case. What I was getting at is that it wasn't born wholly in a single act of creation.) – sbi Jun 20 '11 at 06:37
2

A fowrard declaration declares the identifier (puts it in a namespace) before the actual definition. You need forward declaration of structs if you need to use a pointer to the struct before the struct is defined.

In the context of the answer you linked, if you have typedef struct {...} Foo;, you cannot use a pointer to Foo inside the struct or before the end of the typedef statement.

On the other hand you can typedef struct tagFoo Foo; and later struct tagFoo {...};

John
  • 5,309
  • 1
  • 21
  • 38
  • No need for a new name: typedef struct Foo Foo; struct Foo {}; – Fred Nurk Feb 07 '11 at 21:59
  • Good answer, but note that you can also use forward declarations of classes and structs as function parameters and return values. – Tom Jul 25 '12 at 10:04
0

Forward declaration is needed when a class member uses a reference of another class in it. E.g.:

class AB; // forward declaration
class A {
public:
    int j; 
    void sum(AB a) {
        return a.i + j;
    }
};
class AB{
public:
    int i;
};
Sverri M. Olsen
  • 12,362
  • 3
  • 32
  • 50
Roshni Gandhi
  • 196
  • 12