When to Use a Forward Declaration in C/C++



A forward declaration informs the compiler that a class, function, or variable is declared earlier, but it will be defined later in the code. In this article, our task is to understand the forward declaration and when to use it.

When is Forward Declaration Used in C/C++?

The forward declaration can be used in C/C++ in the following cases:

  • In C++, it is used for declaring a friend function.
  • Using the forward declaration, we can reduce the header files that we include in the code.
  • When two classes are dependent on each other i.e., using each other, then forward declaration can be used to avoid the compilation error. This is explained in the example given below.

Declaring Function/Class without Forward Declaration

The function in C and the class in C++ will throw an error when the function/class is defined later and used before in some other function/class without declaring it first.

Example

In the example below, we have used the add() function in the print() function without any declaration before using the add() function. The add() function is defined later in the code. It will throw an error in the output. Similarly, in the C++ code example, we have used class B before its declaration. It will also throw an error.

#include <stdio.h>

void print()
{
    add(5, 10);
}

void add(int a, int b)
{
    int sum = a + b;
    printf("The sum of %d and %d is: %d\n", a, b, sum);
}

int main()
{
    print();

    return 0;
}

The output of the above code is as follows:

The sum of 5 and 10 is: 15

Warnings/Errors:
main.c: In function 'print':
main.c:8:5: warning: implicit declaration of function 'add' [-Wimplicit-function-declaration]
    8 |     add(5, 10);
#include <iostream>
using namespace std;

class A {
public:
    void display(B&);  
};

class B {
public:
    int value;
    void setData(int n) {
        value = n;
    }
};

void A::display(B& obj) {
    cout << "Value of class B is: " << obj.value << endl;
}

int main() {
    A a;
    B b;
    b.setData(50);
    a.display(b);
    return 0;
}

The output of the above code is as follows:

Warnings/Errors:
main.cpp:8:18: error: 'B' has not been declared
    8 |     void display(B&);
      |       

Declaring Function/Class with Forward Declaration

The declaration of function/class before using it, even if it is defined later on in the code, will not give any error and will be executed without any error.

Example

In this example, we have just defined the add() function before using it in the print() function. The add() function is defined after in the code but still executes without any error. Similarly, we have declared class B before using it and it runs without error.

#include <stdio.h>

void add(int a, int b);
void print();

void print()
{
    add(5, 10);
}

void add(int a, int b)
{
    int sum = a + b;
    printf("The sum of %d and %d is: %d\n", a, b, sum);
}

int main()
{
    print();

    return 0;
}

The output of the above code is as follows:

The sum of 5 and 10 is: 15
#include <iostream>
using namespace std;

class B;  

class A {
public:
    void display(B&);  
};

class B {
public:
    int value;
    void setData(int n) {
        value = n;
    }
};

void A::display(B& obj) {
    cout << "Value of class B is: " << obj.value << endl;
}

int main() {
    A a;
    B b;
    b.setData(50);
    a.display(b);
    return 0;
}

The output of the above code is as follows:

Value of class B is: 50
Updated on: 2025-06-02T18:20:45+05:30

839 Views

Kickstart Your Career

Get certified by completing the course

Get Started