Home > C/C++ > Forward Declarations in C++

Forward Declarations in C++

What are the forward declarations in C++ ?

Simply said, the forward declaration in C++ allows us to declare methods and classes without specifying their actual implementation at a given point of time, leaving that for later if necessary. This can improve the maintainability of the code and help us solve problems related to cyclic dependencies and performance, in certain situations. 


Forward declaration of functions

The first type of forward declaration is the function one. Consider the following example:

The problem with this piece of code is that it’s not going to work.

Error    1    error C3861: 'outputNumber': identifier not found 

The functions above are outside of a class, and the compiler is evaluating them one after another. In order to fix this you got two options, the first of which is moving outputNumber before the main function. The second one is to use forward declaration in the following way:

This will give the compiler a hint that the function in hand actually exists, but the implementation is defined down the execution flow. 


Forward declaration of classes

Let's take this one step further. Consider the following simple example:

Class A:

Class B:

What we do here is to include another header file into our header. The problem here is that in this case it’s unnecessary and will make the compiler include the whole .h class with all the inner information, regardless of its actual usage in our .cpp file. 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. 


Using forward declaration to prevent cyclic dependencies

One of the problems this technique can resolve is the one with the cyclic header dependencies. For example, in the following code:

Class A:

Class B:

The header of class B is included in the header of Class A, and vice versa. As a result, we’ll get an error (which, of course, will not be very descriptive) and the program will not compile. In order to resolve this, we can use forward declaration in the following way:

This will break the cyclic dependency, the header itself should be included in the implementation. 


Limitations of using forward declaration of classes

Needless to say, when we use forward declaration in the header class we can’t use its full functionality. 

Things that we can do using forward declaration:

  • Declare pointers to that class
  • Declare references to that class
  • Declare methods that accept or return objects both by reference and by value
  • Define methods that accept or return references or pointers to the class

Things that we cannot do using forward declaration:

  • Declare stack variables of that type
  • Inherit from that type
  • Define functions that accept/return parameters by value
  • Access any of the class members


So, when to use forward declaration ?

In my opinion – whenever possible.

Use header inclusion only when you’re absolutely sure you need it.  This way you will minimize any inter-header dependencies and even increase compilation time. Sometimes you don’t even need to include the header in the implementation, for example when you are just passing a pointer around. 

An important note is that it's not a good idea to use forward declared types with templates from the standard library. You risk getting undefined behavior even after successful compilation.


About the author:
Kosta Hristov (34 Posts)

Hi there ! My name is Kosta Hristov and I currently live in London, England. I've been working as a software engineer for the past 6 years on different mobile, desktop and web IT projects. I started this blog almost one year ago with the idea of helping developers from all around the world in their day to day programming tasks, sharing knowledge on various topics. If you find my articles interesting and you want to know more about me, feel free to contact me via the social links below. ;)

Like the article ? Share it ! ;)

  1. Sonu Maddeshia
    December 26th, 2013 at 12:58 | #1

    Nice tutorial : )

  1. No trackbacks yet.

Current month ye@r day *

Copyright © Developing the future 2013. Licensed under the CC> BY-NC-ND 3.0 Creative Commons license.       
Audi R8 wallpapers Sony Xperia Z4 Tablet WiFi