Advertisement

[C++] Forward declaration VS include in headers

Started by January 06, 2014 04:26 PM
2 comments, last by Melkon 11 years, 1 month ago

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

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

Advertisement

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.

BSc Computer Games Programming (De Montfort University) Graduate

Worked on Angry Birds Go! at Exient Ltd

Co-founder of Stormburst Studios

edit: nevermind

This topic is closed to new replies.

Advertisement