Questions tagged [forward-declaration]

Forward declarations allow statically-typed programs to indicate the type and name of a symbol without actually defining it.

Forward declarations are how references to an undefined symbol can be avoided in a statically-typed language. Forward declarations allows the compiler to know the name and type of a symbol without the program defining it previously.

However, forward declarations (when declaring structs and classes) are incomplete, which places special restrictions on what can and cannot be done with a forward declared type.

For example, consider the following case (C++):

class Parent
{
    // ...
    vector<Child> children;
};

class Child
{
    // ...
    Parent *parent;
}; 

When the compiler compiles the definition of parent, it finds a reference to the type Child, which is undefined, causing the compilation to fail.


A forward declaration alleviates this issue:

class Child;
class Parent
{
    // ...
    vector<Child> children;
};


class Child
{
    // ...
    Parent *parent;
}; 

When the forward declaration is added, the compiler is aware that there will be a Child class somewhere later in the program, and compiles successfully.

972 questions
51
votes
3 answers

Objective-C: Forward Class Declaration

I'm writing a multiview app that utilizes a class called RootViewController to switch between views. In my MyAppDelegate header, I create an instance of the RootViewController called rootViewController. I've seen examples of such where the @class…
Old McStopher
  • 6,085
  • 9
  • 58
  • 84
51
votes
3 answers

Why does a C++ friend class need a forward declaration only in other namespaces?

Suppose I have a class F that should be friend to the classes G (in the global namespace) and C (in namespace A). to be friend to A::C, F must be forward declared. to be friend to G, no forward declaration of F is necessary. likewise, a class A::BF…
pesche
  • 2,794
  • 2
  • 32
  • 35
46
votes
3 answers

forward declaration of a struct in C?

#include struct context; struct funcptrs{ void (*func0)(context *ctx); void (*func1)(void); }; struct context{ funcptrs fps; }; void func1 (void) { printf( "1\n" ); } void func0 (context *ctx) { printf( "0\n" ); } void…
user1128265
  • 2,511
  • 10
  • 26
  • 32
46
votes
4 answers

c++ "Incomplete type not allowed" error accessing class reference information (Circular dependency with forward declaration)

Had some issues in my code recently surrounding what I now know of as a Circular dependency. In short there are two classes, Player and Ball, which both need to use information from the other. Both at some point in the code will be passed a…
nat1707828
  • 697
  • 2
  • 9
  • 16
44
votes
12 answers

Forward declarations of unnamed struct

Bounty question: So, these two Foos aren't the same thing. Fine. The second form is given in a library. How do I forward-declare it given that I can't change it? I always thought C and C++ allowed repeated declarations provided that there were no…
spraff
  • 29,265
  • 19
  • 105
  • 197
42
votes
3 answers

"Implicit instantiation of undefined template" when forward declaring template class

I've got some code in which I need to forward-declare the a template class (or at least, forward-declaring would make things a lot easier for me...). I've written a simplified version of the problem I'm having so I can display it…
benwad
  • 5,676
  • 8
  • 52
  • 89
35
votes
6 answers

C++ nested class/forward declaration issue

Is it possible to forward-declare a nested class, then use it as the type for a concrete (not pointer to/reference to) data member of the outer class? I.E. class Outer; class Outer::MaybeThisWay // Error: Outer is undefined { }; class Outer { …
uj2
  • 1,365
  • 2
  • 15
  • 15
32
votes
3 answers

Why can't I use a Javascript function before its definition inside a try block?

As discussed here, function definitions can be used before they're defined. But as soon as a section of code is wrapped in a try block, this ceases to be the case. This displays "Hello world": hello(); function hello() { alert("Hello world"); } But…
Chris Noe
  • 33,647
  • 22
  • 66
  • 90
32
votes
2 answers

What is a parameter forward declaration?

I thought I knew C syntax quite well, until I tried to compile the following code: void f(int i; double x) { } I expected the compiler to trip, and it did, but I don't get the error message: test.c:1:14: error: parameter ‘i’ has just a forward…
Fred Foo
  • 328,932
  • 68
  • 689
  • 800
29
votes
9 answers

Managing forward declarations

It's well known that using forward declarations is preferable to using #includes in header files, but what's the best way to manage forward declarations? For a while, I was manually adding to each header file the forward declarations that were…
Josh Kelley
  • 50,042
  • 19
  • 127
  • 215
29
votes
6 answers

How to forward typedef'd struct in .h

I have Preprocessor.h #define MAX_FILES 15 struct Preprocessor { FILE fileVector[MAX_FILES]; int currentFile; }; typedef struct Preprocessor Prepro; void Prepro_init(Prepro* p) { (*p).currentFile = 0; } I realized then that I had to…
erandros
  • 1,645
  • 3
  • 17
  • 42
28
votes
3 answers

Delete objects of incomplete type

This one made me think: class X; void foo(X* p) { delete p; } How can we possibly delete p if we do not even know whether X has visible destructor? g++ 4.5.1 gives three warnings: warning: possible problem detected in invocation of delete…
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
27
votes
6 answers

Why, really, deleting an incomplete type is undefined behaviour?

Consider this classic example used to explain what not to do with forward declarations: //in Handle.h file class Body; class Handle { public: Handle(); ~Handle() {delete impl_;} //.... private: Body…
27
votes
9 answers

What are the dangers of forward declarations?

I just had an interview. I was asked what is a "forward declaration". I was then asked if they were dangers associated with forward declarations. I could not answer to the second question. A search on the net didn't show up any interesting…
Offirmo
  • 16,196
  • 8
  • 69
  • 90
26
votes
4 answers

Templates: Use forward declarations to reduce compile time?

I have to deal with a library that consists of many templated classes, which are of course all implemented in header files. Now I'm trying to find a way to reduce the unbearably long compile times that come from the fact that I pretty much have to…
Frank
1
2
3
64 65