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:

#include "stdafx.h"
#include <iostream>
 
using namespace std;
 
int main()
{
	outputNumber(5);
 
    return 0;
}
 
void outputNumber(int number)
{
    cout << number;
}

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:

#include "stdafx.h"
#include <iostream>
 
using namespace std;
 
void outputNumber(int number);
 
int main()
{
	outputNumber(5);
 
    return 0;
}
 
void outputNumber(int number)
{
    cout << number;
}

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:

#include "B.h"
 
class A
{
public:
	A(void);

	~A(void);
 
private:
	B *aMember;
};

Class B:

class B
{
public:
	B(void);
	~B(void);
 
	// more methods here
};

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. 

class B;
 
class A
{
public:
	A(void);
	~A(void);
 
private:
	B *aMember;
};

 

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:

#include "B.h"
 
class A
{
public:
	A(void);
	~A(void);
 
private:
	B *aMember;
};

Class B:

#include "A.h"
 
class B
{
public:
	B(void);
	~B(void);
 
	// more methods here
 
private:
	A *aMember;
};

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:

class B;
 
class A
{
public:
	A(void);
	~A(void);
 
private:
	B *aMember;
};

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.


 



Like the article ? Share it ! ;)


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

    Nice tutorial : )

  1. No trackbacks yet.


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