Relying on #includes from headers

Started by
12 comments, last by EddieV223 10 years, 8 months ago

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.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

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.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

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.

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.

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

// Easy to learn 2D Game Library c++

SFML2.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

This topic is closed to new replies.

Advertisement