Archived

This topic is now archived and is closed to further replies.

NextS

#include vs forward declarations

Recommended Posts

Hi Everyone. I once stumbled over a Paper covering C++ codedesign which included an intersting paragraph concerning header #inclusion vs forward declaration. By the way, this is all about headers in header inclusion. Saying: Put all includes you may need in your .cpp file into the .h file. Until now I used to do it right the other way which seemed perfect for me. (Only including the absolute neccessary files in the header, forward declaring parameters and pointers in the class''s header, including the whole stuff in the cpp file ). In my opinion my approach had some advantages: - Removing the includes from the header reduced dependencies so that compile speed enhanced quite a bit. - Also if you wish to modularize your code you have the advantage of already having the neccessary includes in you .cpp file. You would else depend much more on the order of inclusion. - If different modules use different versions of the same header file then problems are preprogrammed (duplicate symbols, redefinitios) (e.g. wsock.h and wsock2.h) - The header file as i understand it is an interface for a user. An interface has the property of only displaying minimal information to the client (programmer in this case) so he needs not have knowledge of the internal functionality Maybe there are more advantages but what really concerns me are the disadvantages. The papers one and only negative point was that forward declarations would make the code less readable? . In my opinion (or in my coding style) this is not true especially when you use descriptive names for your classes. Well yes, if you have the header file and only the header file, then you have no idea in which files the forward declared classes are stored. But there''s conventions for that too. (having a .cpp and .h file for each class the name beeing the name of the class... ) Just my 2 cents P.S. Sorry for not having a reference to the document for this document is company internal. But it said it would have taken the coding style suggestions from SGI''s suggestions... .

Share this post


Link to post
Share on other sites
Sounds like the paper you read was full of crap. The only good reason for putting all the includes in the header is if you''re using a compiler that is going to precompile that header. Even that usually requires that you use the same header in pretty much every source file, and always at the very top, etc.

How can forward declarations be considered ''unreadable''? Put a comment at the top saying "// forward declarations" if you have amateur programmers who can''t tell just by looking at them.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
I wouldn''t put everything into a header file only those things the header needs to have. I would then include more headers into the c++ file after I included primary or class header first. I would also forward declare my data to avoid circular problems with headers. Only system headers and headers that you won''t touch anymore should be precompiled. Your c++ file should include headers the c++ file needs to use and not having those headers go with your header into other c++ files since they''re not using names as your first c++ file.

Share this post


Link to post
Share on other sites