Jump to content

  • Log In with Google      Sign In   
  • Create Account


[C++] Forward declaration VS include in headers


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Melkon   Members   -  Reputation: 461

Like
0Likes
Like

Posted 06 January 2014 - 10:26 AM

Hello!

 

Is there any issue with using forward declaration in header files instead of includes when it's possible?

It will compile faster, however i am not entirely sure if it's a good idea. ohmy.png

 

Thank you for answers!

Melkon



Sponsor:

#2 fastcall22   Crossbones+   -  Reputation: 3964

Like
7Likes
Like

Posted 06 January 2014 - 11:09 AM

Yes, it is almost always a good idea to forward declare wherever possible.  Remember that header files and source files are, respectively, the interface and the implementation.  Any file that includes header file will also include its includes.  If you have an header that includes a rather expensive file, then those that also include that header will also take longer to compile.  If you move the expensive include to the implementation, and forward declare in the header, then only the source file will take longer to complile.  Additionally, you also prevent circular inclusions.  See problem #2 in Organizing Code Files in C and C++.


WW91J3ZlIGdvdCBhIHNlY3JldCBib251cyBwb2ludCE=


#3 Dezachu   Members   -  Reputation: 350

Like
0Likes
Like

Posted 06 January 2014 - 11:42 AM

Depends what you're wanting to forward declare I guess.

 

There's a time and a place for both (includes and forward declarations). Use forward decls where possible, but don't be afraid to use includes if you need to.


Studying BSc Computer Games Programming @ De Montfort University, Leicester.

On a year long placement as a Junior Programmer at Exient Ltd, Oxford (worked on the recently released Angry Birds Go!) :)


#4 Melkon   Members   -  Reputation: 461

Like
0Likes
Like

Posted 07 January 2014 - 02:32 AM

edit: nevermind


Edited by Melkon, 07 January 2014 - 02:50 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS