Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward Declaration vs Include

Tags:

Consider the following two scenarios (Edited just to complete the whole question and make it clearer)

Case 1: (doesnt compile as rightly mentioned below)

//B.h #ifndef B_H #define B_H #include "B.h"  class A;  class B {          A obj;         public:         void printA_thruB();           };   #endif  //B.cpp #include "B.h" #include <iostream>  void B::printA_thruB(){         obj.printA();         }      //A.h; #ifndef A_H #define A_H  #include "A.h"  class A {          int a;         public:         A();         void printA();           };   #endif     //A.cpp                            #include "A.h"                     #include <iostream>                 A::A(){                                    a=10;                              }                           void A::printA()                   {                                  std::cout<<"A:"<<a<<std::endl;     }     //main.cpp  #include "B.h"   #include<iostream>  using namespace std;   int main()  {  B obj;  obj.printA_thruB();  } 

Case 2: (the only modifications...works without compiliation error)

//B.h  #include "A.h" //Add this line //class A;     //comment out this line 

Let us assume both the A.cpp and B.cpp are complied together. Do the above two scenarios make any differences? Is there a reason to prefer one method over the other?

Edit: So how do I make scenario 1 work.

like image 644
Sii Avatar asked Sep 03 '10 03:09

Sii


People also ask

Should I use forward declaration or include?

As the name itself implies, forward declaration is just a Declaration and not a definition. So, you will declare saying the compiler that it is a class and I just declaring it here and will provide you the definition when am gonna use it. So, normally you forward declare in the Header file and #include in the .

Why use forward declaration instead of include?

A forward declaration is much faster to parse than a whole header file that itself may include even more header files. Also, if you change something in the header file for class B, everything including that header will have to be recompiled.

What is the advantage of forward declaration?

Forward declarations in C++ are useful to save in compile time as the compiler does not need to check for translation units in the included header. Also it has other benefits such as preventing namespace pollution, allowing to use PImpl idiom and it may even reduce the binary size in some cases.

What is forward declaration of a function?

In computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition.


1 Answers

Forward declaration is not a substitute for Header file inclusion.

As the name itself implies, forward declaration is just a Declaration and not a definition.

So, you will declare saying the compiler that it is a class and I just declaring it here and will provide you the definition when am gonna use it. So, normally you forward declare in the Header file and #include in the .cpp file where you will use the members of the forward declared class.

By doing so, what you make is, wherever you are including the header file there will just be a declaration for the class instead of the entire contents #included...

But having said that, when the compiler requires the definition of the class, it should be #included..

So, in your case A obj; requires the definition of class A and hence you should #include..

I myself asked a similar question here and another similar question which has also a nice answer...

Hope it helps..

like image 199
liaK Avatar answered Oct 01 '22 14:10

liaK