Jump to content
  • Advertisement
Sign in to follow this  
dmatter

Relying on #includes from headers

This topic is 2230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

As a style, always program to an interface.  

 

The header files for those interfaces absolutely should not include headers like <string>.  An interface does not require it.

 

Then how do you use std::string in your class declaration?

I don't think we should forward declare symbols in third party or even system libraries.

Share this post


Link to post
Share on other sites
Advertisement

To answer OP's question, here is how I do in my code,

1, For the declarations (such as classes) in my code, I forward declare them whenever possible, in the header. Of course the source file needs to include the corresponding header, that's fine.

2, For the declarations in third party and system library, I never forward declare them. If I do, I will get a lot of trouble if the library changes the name or namespace.

3, I always try to avoid #include in the header (my point #1), but if I can't avoid, just include it (my point 2), no problem.

Share this post


Link to post
Share on other sites

I do everything I can to stop includes in headers, and use forward declarations instead. But if I need the #include in the header, I won't put it in the source file, it already has it in the header so it doesn't need it. If you don't see the #include for whatever you're using, it most likely is in the header. I despise multiple includes when they're not needed, it's equivelant to the sound of Styrofoam rubbing against other Styrofoam to me. 

Share this post


Link to post
Share on other sites

When you include <string> in a header it's because it must be there, you have no option to forward declare.  Since you know that this header will be in the cpp there is no reason to reinclude it in the cpp file, its already part of the classes declaration.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!