Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


[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: 549

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: 5748

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++.


QWxsIHRvYXN0LXRvYXN0aW5nIHRvYXN0ZXJzIGNhbiB0b2FzdCB0b2FzdGVkIHRvYXN0LCBhbHRob3Vn aCByZS10b2FzdGluZyB0b2FzdGVkIHRvYXN0IGlzIGdlbmVyYWxseSBub3QgcmVjb21tZW5kZWQgYnkg dGhlIG1hbnVmYWN0dXJlcnMgb2YgdG9hc3QtdG9hc3RpbmcgdG9hc3RlcnMuLi4=

#3 Dezachu   Members   -  Reputation: 488

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.

Completed a placement as a Junior Programmer at Exient Ltd in Oxford/Valletta, Malta


#4 Melkon   Members   -  Reputation: 549

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