-1

I've recently run into the idea of forward declarations within C++. I have read that by using forward declarations, compile times can be quickened by avoiding unnecessary declarations and recursive #includes. However, this has got me wondering, why exactly does anyone use #includes then?

Are there situations where plain forward declarations just aren't reasonable? Such as if you have a library header file, it would just make more sense to have a single #include then naming each class/function prototype?

I've read many posts on stack overflow explaining the differences between each path, but why exactly use #includes?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Izzo
  • 4,461
  • 13
  • 45
  • 82
  • Forward declarations are not always sufficient. – Sam Varshavchik Mar 29 '16 at 02:02
  • Comparing apples and oranges. These are completely different concepts. You really think copy/paste is less error prone than a single `#include`? Wow! – too honest for this site Mar 29 '16 at 02:03
  • See [When can I use a forward declaration?](http://stackoverflow.com/q/553682/3309790) and [C++ - Forward declaration](http://stackoverflow.com/q/4757565/3309790). – songyuanyao Mar 29 '16 at 02:04
  • @SamVarshavchik That's what OP is asking. Your comment is unhelpful in this regard. – Disillusioned Mar 29 '16 at 02:04
  • 2
    The declarations in `#include` files **are** forward declarations. They're just kept in a file that comes with the library they describe. – Barmar Mar 29 '16 at 02:18
  • 1
    To indicate the correctness of statements in the question, I don't think downvoting is appropriate, instead you should vote current answers or even add your own answer, so that it can help the question owner above, and even other visitors that holds the same misconceptions. – ggrr Mar 29 '16 at 02:22
  • 1
    You may find this article about headers and includes helpful to clear up some misconceptions: http://www.cplusplus.com/forum/articles/10627/ – Disillusioned May 21 '16 at 03:06

2 Answers2

2

The main reason for including headers, rather than using forward declarations throughout your code, is enforcing consistency with minimal duplication.

Consider a header used in several cpp files. When a class or a variable changes in the header, you would need to find and change all references in all files. Otherwise, the code would continue compiling, but it would fail to link.

In addition, forward declarations have their limits: they let you declare pointer to classes or call functions, but they do not allow creating instances or accessing their members.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • This is interesting. Regarding your last statement that they only let you declare pointers to the forward declared classes/functions... This is due to the fact that most forward declarations only mention the function name/class name and not the rest of the interface? That is, unless you explicitly declare the public/private/protected members, there's no way you could ever reference them in your code? In this case, it would be more practical convenient to have a single header file make all of those declarations? – Izzo Mar 29 '16 at 02:14
  • @Teague Correct, forward-declared class does not specify members, so you cannot access them. Header has a complete definition, letting you access members as needed. – Sergey Kalinichenko Mar 29 '16 at 09:44
1

It is simply that forward declarations you have in mind cannot replace declarations in headers.

One obvious example is the declaration of class:

Assuming I have a Foo class and is going to contains Bar:

This will work

// FOO.H
class Bar;

class Foo {
private:
    Bar* bar;
};

But this won't:

// FOO.H
class Bar;

class Foo {
private:
    Bar bar;
};

There is a lot of cases that compiler needs to know the "full" declaration (which we usually put in headers) in order to do its work, instead of just a forward declaration containing the name of class etc (which allow you to mostly create pointer/reference to such class).

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131