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.

1070 questions
688
votes
13 answers

When can I use a forward declaration?

I am looking for the definition of when I am allowed to do forward declaration of a class in another class's header file: Am I allowed to do it for a base class, for a class held as a member, for a class passed to member function by reference, etc.…
Igor
  • 26,650
  • 27
  • 89
  • 114
304
votes
19 answers

Forward declaring an enum in C++

I'm trying to do something like the following: enum E; void Foo(E e); enum E {A, B, C}; which the compiler rejects. I've had a quick look on Google and the consensus seems to be "you can't do it", but I can't understand why. Can anyone…
szevvy
  • 3,446
  • 2
  • 18
  • 10
290
votes
8 answers

What are forward declarations in C++?

At this link, the following was mentioned: add.cpp: int add(int x, int y) { return x + y; } main.cpp: #include int add(int x, int y); // forward declaration using function prototype int main() { using namespace std; cout…
Simplicity
  • 47,404
  • 98
  • 256
  • 385
279
votes
11 answers

Forward declaration of a typedef in C++

Why won't the compiler let me forward declare a typedef? Assuming it's impossible, what's the best practice for keeping my inclusion tree small?
user96825
  • 2,791
  • 2
  • 16
  • 4
266
votes
17 answers

How do I forward-declare a function to avoid `NameError`s for functions defined later?

Is it possible to forward-declare a function in Python? I want to sort a list using my own cmp function before it is declared. print "\n".join([str(bla) for bla in sorted(mylist, cmp = cmp_configs)]) I've put the definition of cmp_configs method…
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
238
votes
7 answers

Forward declaration of nested types/classes in C++

I recently got stuck in a situation like this: class A { public: typedef struct/class {…} B; … C::D *someField; } class C { public: typedef struct/class {…} D; … A::B *someField; } Usually you can declare a class name: class…
Calmarius
  • 18,570
  • 18
  • 110
  • 157
217
votes
9 answers

receiver type *** for instance message is a forward declaration

In my iOS5 app, I have NSObject States class, and trying to init it: states = [states init]; here is init method in States: - (id) init { if ((self = [super init])) { pickedGlasses = 0; } return self; } But there is error…
SentineL
  • 4,682
  • 5
  • 19
  • 38
206
votes
3 answers

How do I forward declare an inner class?

I have a class like so... class Container { public: class Iterator { ... }; ... }; Elsewhere, I want to pass a Container::Iterator by reference, but I don't want to include the header file. If I try to forward declare the…
bradtgmurray
  • 13,683
  • 10
  • 38
  • 36
149
votes
4 answers

How to forward declare a C++ template class?

Given a template class like the following: template class Mappings { public: ... Type valueFor(const IDType& id) { // return value } ... }; How can someone forward declare this class…
Tron Thomas
  • 1,501
  • 2
  • 9
  • 3
110
votes
3 answers

Forward function declarations in a Bash or a Shell script?

Is there such a thing in bash or at least something similar (work-around) like forward declarations, well known in C / C++, for instance? Or there is so such thing because for example it is always executed in one pass (line after line)? If there are…
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
98
votes
1 answer

Forward declaration with unique_ptr

I have found it useful to use forward declaration of classes in combination with std::unique_ptr as in the code below. It compiles and works with GCC, but the whole thing seem kind of strange, and I wonder if this is standard behaviour (i.e.,…
Zyx 2000
  • 1,625
  • 1
  • 12
  • 18
97
votes
9 answers

Should one use forward declarations instead of includes wherever possible?

Whenever a class declaration uses another class only as pointers, does it make sense to use a class forward declaration instead of including the headerfile in order to pre-emptively avoid problems with circular dependencies? so, instead of…
Mat
  • 4,281
  • 9
  • 44
  • 66
97
votes
3 answers

What is the header?

What's the header used for? Why is it necessary? Any example?
wp2
  • 1,321
  • 3
  • 11
  • 10
89
votes
5 answers

In C++, is it possible to forward declare a class as inheriting from another class?

I know that I can do: class Foo; but can I forward declare a class as inheriting from another, like: class Bar {}; class Foo: public Bar; An example use case would be co-variant reference return types. // somewhere.h class RA {} class RB :…
anon
  • 41,035
  • 53
  • 197
  • 293
68
votes
2 answers

error: member access into incomplete type : forward declaration of

I have two classes in the same .cpp file: // forward class B; class A { void doSomething(B * b) { b->add(); } }; class B { void add() { ... } }; The forward does not work, I cannot compile. I get this error:…
LOLKFC
  • 1,065
  • 1
  • 9
  • 18
1
2 3
71 72