What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Understand Forward Declarations in C++ (with Examples)

  • Apr 13, 2023
  • 6 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Abrar Ahmed
Understand Forward Declarations in C++ (with Examples)

C++ is prominent for its power, versatility, and efficiency, but can be a little complicated language to newcomers. Forward declaration is one such complex topic that can be difficult for newbies to grasp. In this article, we will define forward declaration, discuss its benefits, explain why declarations are required in C++, and show instances of forward declaration in C++.

What is Forward Declaration in C++?

Forward declaration in C++ allows you to declare an identifier before it is defined. This means you can inform the compiler about a class, function, or variable before it is used in code. A declaration statement without a definition is used to achieve this.

A declaration statement typically describes the data type, name, and, in some situations, value of an identifier. For instance, the statement "int a;" declares an integer variable named "a". The data type of "int" is assigned to the identifier "a," however its value is not defined.

A definition statement, on the other hand, describes both the data type and the value of an identifier. The phrase "int a = 5;" for example, not only defines an integer variable named "a," but also sets its value to 5.

Advantages of C++ Forward Declaration

The biggest benefits of C++ forward declaration is to improve compilation time, reduce coupling, and break circular dependencies.

Let's learn about them one by one:

  1. Improving Compilation Time: Forward declaration can reduce the time required for compilation by reducing the amount of code the compiler has to process. Header files play a big impact on compilation time. By using forward declaration you can reduce the number of header files in the code, thereby improving the compilation process.
  2. Reducing coupling: Debugging when incorporating various modules into one code is another problem that can be avoided with a forward declaration. It limits the number of dependencies that exist between modules in general. This can also help in reducing the coupling between different parts of the code.
  3. Breaking circular dependencies: If two classes have a circular dependency in which one class refers to the other, you can break the cycle using forward declaration. You can tell the compiler about the existence of a class without supplying its full definition, which can be defined later, by forward-declaring it. 

Let's also check a simple example to understand how it breaks dependencies:

#include<iostream>

using namespace std;

class B {

public:

int x;

A obja;

};

class A {

public:

int y;

B objb;

};




int main()

{

B obj1;

A obj2;

}

 

In this code, an error is thrown. What happens is, as the compiler is compiling the code, it comes to the first class B according to the order in which the code is written. It observes in class B, that there is an object of class A. But it has not encountered any mention of class A whatsoever.

This causes it throws an exception saying Class A not found. Even if we were to swap the places of the class definitions for class A and class B. We would encounter the same error. We can avoid this error by forward declaring both class A and class B. In that manner, the compiler will know by default that there are two different classes A and class B. The following code shows this implementation:

#include<iostream>

using namespace std;

class A;

class B;

class B {

public:

int x;

A obja;

};

class A {

public:

int y;

B objb;

};




int main()

{

B obj1;

A obj2;

}

 

Why are Declarations Needed in C++?

Declarations are necessary for C++ because they inform the compiler that an identifier exists. Before a variable, function, or class can be utilized, the compiler must know its data type and name. This data is needed to allocate memory, check type accuracy, and produce object code.

Additionally, because C++ uses a compiler, all identifiers must be specified to the compiler before the code can be generated. C++, unlike interpreted languages like Python or JavaScript, cannot determine the data type of a variable or function by studying the code at runtime. This is why C++ requires declarations.

Now, we should look at take a look at some examples of forward declaration in C++.

Forward Declaring a Class

Here is the code for the forward declaration of a class in C++:

class MyClass; // Forward Declaration

class MyOtherClass

{

public:

MyClass* myObject;

};

class MyClass

{

public:

int myValue;

};

 

In this example, we forward-declare the class "MyClass" before it is defined. This allows us to use a pointer to the "MyClass" object in the "MyOtherClass" definition without providing the full definition of “MyClass”. We can define "MyClass" later in the code, and it will not affect the use of the pointer.

 

Forward Declaring a Function

Here is the code for the function's forward declaration:

void myFunction(int a, float b); // Forward

 

In this example, we forward-declare the function "myFunction" before it is defined. This allows us to use the function in other parts of the code without providing its full definition. We can define "myFunction" later in the code, and it will not affect its use.

Forward Declaring a Variable

Here is the code for the forward declaration of a variable:

class MyClass;

extern MyClass myObject; // Forward Declaration

class MyClass

{

public:

int myValue;

};

MyClass myObject = MyClass();

 

In this example, we forward-declare the class "MyClass" before it is defined. We also forward-declare a variable "myObject" of type "MyClass" using the "extern" keyword. This tells the compiler that "myObject" is defined elsewhere and that we will provide the full definition later. We can then define "MyClass" and "myObject" later in the code.

Still, we have some doubts, we have experts for C++ homework help to solve them.

Conclusion

Overall, we understood forward declaration in C++ that allows you to declare an identifier before it is defined. Declarations are crucial because they supply the compiler with information about the presence of an identifier. You may develop more modular and maintainable code by utilizing forward declaration. Happy Learning :)

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Abrar Ahmed
An ambivert individual with a thirst for knowledge and passion to achieve. Striving to connect Artificial Intelligence in all aspects of life. I am also an avid coder and partake in coding challenges all the time on Leetcode and CodeChef.