Should one use forward declarations instead of includes wherever possible?

forward declaration c++
when to use forward declarations
c++ forward declaration class member
forward declaration c++ header
the forward declaration of a function requires select all answers that apply
forward declaration of struct
c++ forward declaration function
go forward declaration

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 having:

//file C.h
#include "A.h"
#include "B.h"

class C{
    A* a;
    B b;
    ...
};

do this instead:

//file C.h
#include "B.h"

class A;

class C{
    A* a;
    B b;
    ...
};


//file C.cpp
#include "C.h"
#include "A.h"
...

Is there any reason why not to do this wherever possible?

What are forward declarations in C++?, would suffice. When you include a header file you introduce a dependency that will cause your code to be recompiled whenever the header file changes. A forward declaration is much faster to parse than a whole header file that itself may include even more header files. Also, if you change something in the header file for class B, everything including that header will have to be recompiled. With a forward declaration, that may only be the source file where A's implementation is residing in.

What are Forward declarations in C++, Should one use forward declarations instead of includes wherever possible? Is there any reason why not to do this wherever possible? When working with a library, whether it be my own or external, there are lot of classes with forward declarations. The same classes also have includes as well depending on the situation. When I use a certain class, I need to know whether certain objects that the class uses, are forward declared or are they

Is there any reason why not to do this wherever possible?

Convenience.

If you know ahead of phase that any user of this header file will necessarily need to also include the definition of A to do anything (or perhaps most of the times). Then it is convenient to just include it once and for all.

This is a rather touchy subject, as a too liberal use of this rule of thumbs will yield a nigh uncompilable code. Note that Boost approaches the problem differently by providing specific "convenience" headers which bundles a couple of close functionalities together.

Forward Class Declaration in C++, Imagine that you have two classes: MyClassA and MyClassB. to reference MyClassA in MyClassB, do you know where you should use #include a method argurment, you can simply use the forward declaration instead. Use #include "​MyClassA.h" in the header file of MyClassB.h whenever you will be  It needs one if we use one of its members or the base class, or if it has to know how large objects of that class are. One should think that the same applies to enums, but that’s not the case. Forward-declaring plain old enums is not possible. The good news is, that we can provide forward declarations for scoped enums aka. enum classes. We

One case in which you don't want to have forward declarations is when they are themselves tricky. This can happen if some of your classes are templated, like in the following example:

// Forward declarations
template <typename A> class Frobnicator;
template <typename A, typename B, typename C = Frobnicator<A> > class Gibberer;

// Alternative: more clear to the reader; more stable code
#include "Gibberer.h"

// Declare a function that does something with a pointer
int do_stuff(Gibberer<int, float>*);

Forward-declarations are the same as code duplication: if the code tends to change a lot, you have to change it in 2 places or more each time, and that is no good.

Struct declaration, Use an incomplete declaration in a header file whenever possible. that contains the incomplete declaration of X. A.cpp will be the it can make full use of X. Instead of "class or struct", we'll just say "class" in what follows. Forward declaration is used in languages that require declaration before use; it is necessary for mutual recursion in such languages, as it is impossible to define such functions (or data structures) without a forward reference in one definition: one of the functions (respectively, data structures) must be defined first. It is also useful to allow flexible code organization, for example if one wishes to place the main body at the top, and called functions below it.

Should one use forward declarations instead of includes wherever , A forward declaration in C++ is when you declare something before its You cannot do much with an incomplete type besides use pointers (or If Bar was in its own header file, we would need to include it in Foo's header file: it a challenge to replace them with forward declarations wherever I could. o Avoid gratuitous #includes. Use forward declarations whenever a definition isn't required. (This may sound obvious to experienced programmers, but it's trickier than you might think for templates.) o Avoid unnecessary membership. Use the Pimpl Idiom to fully hide a class' private implementation details.

How to use Forward Declaration in C++, Gregory Popovitch said it all, but I want to further explain what circular Now B shall be a child of C. Let's include B's header in C. Now, suddenly you realize that Another benefit is that if the class declaration header is included everywhere, any Finally, if you use forward declarations, you are much less likely to have the  This is considered bad practice and is unnecessary, because we don’t use any of the external class's members, we simply declare a pointer to it. A better option would be to use forward declaration for the class, and include the actual header in the implementation if necessary.

[PDF] Using Incomplete (Forward) Declarations, using forward declaration in header files instead of includes when it's possible​? It will compile faster, however i am not entirely sure if it's a good idea. Yes, it is almost always a good idea to forward declare wherever possible. There's a time and a place for both (includes and forward declarations). Use forward declarations whenever possible It’s a really trivial piece of advice, but remember that using forward declarations instead of including the header of corresponding class is better because not only does it minimize the compile time, it also simplifies the dependencies between different source files.

The joys of forward declarations: results from the real world – Andy , Should I be making DTO like class instead and the use that for the As for forward declarations, I use them as needed whenever you have circular I personally use include statetemnts in header files as least as possible and  Finally, if you use forward declarations, you are much less likely to have the annoying problem of Circular dependency, where header a.h needs to include b.h and b.h needs to include a.h. Personally, I strive to use forward declarations whenever possible.

Comments
  • uhm - is that the answer to the question on the top or on the bottom?
  • your real question (at the bottom) - AFAIK there is no reason not to use a forward declaration in this case...
  • It slightly depends what you mean by "uses another class only as pointers". There's a nasty case where you can delete a pointer using only a forward declaration, but if the class in fact has a non-trivial destructor then you get UB. So if delete "only uses pointers" then yes, there's a reason. If it doesn't count, not so much.
  • Isn't the circular dependancy still there and just hidden from the compiler? If yes both tactics always including and always doing forward declaration don't teach you how to avoid circular dependencies. However I must admit that with forward declarations they may be easier to find.
  • If class is A actually defined as a struct, some compilers may complain. If class A is a derived class, you've got problems. If class A is defined in another namespace and the header just pulls it into this one with a using declaration, you may have problems. If class A is actually an alias (or a macro!), you've got problems. If class A is actually a typedef, you've got problems. If class A is actually a class template with default template parameters, you've got problems. Yes, there is a reason not to forward declare: it breaks encapsulation of implementation details.
  • Also, increased chance of recompilation.
  • "I can't think of a situation where including a file where you can use a forward declaration is better" - when the forward declaration yields UB, see my comment on the main question. You're right to be cautious, I think :-)
  • @Luchian: Because whether it's an answer or not depends what the questioner originally meant, I don't want to post "answers" that amount to quibbling. Maybe the questioner would never dream of writing a delete statement in C.h.
  • A downside is more work and more code! And more fragility. You can't possibly say there are no downsides.
  • What if there are 5 classes? What if you later need to add some? You just focused on the best possible case for your point.