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
631
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 Oks
  • 24,930
  • 25
  • 85
  • 112
254
votes
10 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,541
  • 2
  • 15
  • 4
236
votes
8 answers

What are forward declarations in C++?

At: http://www.learncpp.com/cpp-tutorial/19-header-files/ The following is 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…
Simplicity
  • 41,347
  • 81
  • 231
  • 358
214
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
  • 16,155
  • 15
  • 95
  • 134
212
votes
17 answers

Is it possible to forward-declare a function in Python?

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 organized my code to put the definition…
Nathan Fellman
  • 108,984
  • 95
  • 246
  • 308
207
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,542
  • 4
  • 17
  • 38
177
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
  • 12,395
  • 9
  • 33
  • 36
116
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,171
  • 2
  • 7
  • 3
100
votes
2 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
  • 35,473
  • 22
  • 102
  • 177
83
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,445
  • 1
  • 11
  • 16
80
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
  • 3,819
  • 9
  • 41
  • 66
80
votes
3 answers

What is the header?

What's the header used for? Why is it necessary? Any example?
wp2
  • 1,151
  • 3
  • 9
  • 10
74
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
  • 36,629
  • 47
  • 184
  • 286
66
votes
7 answers

Does int main() need a declaration on C++?

When reading about functions in C++, I was taught that functions need declarations to be called. For example: #include int main() { std::cout << "The result is " << sum(1, 2); return 0; } int sum(int x, int y) { return x +…
Vini Brasil
  • 4,828
  • 3
  • 22
  • 31
54
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
  • 925
  • 1
  • 8
  • 18
1
2 3
64 65