What are forward declarations in C++?


Forward declaration lets the code following the declaration know that there is are classes with the name Person. This satisfies the compiler when it sees these names used. Later the linker will find the definition of the classes.

 example

Class Person;

void myFunc(Person p1) {
   // ...
}
Class Person {
   // Class definition here
};

So in this case when the compiler encounters myFunc, it'll know that it's going to encounter this class somewhere down in the code. This can be used in cases where code using the class is placed/included before the code containing the class definition.

Updated on: 12-Feb-2020

332 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements