Pregunta

I've seen some C++ projects (which are quite large, but not much) in many source code hosting sites, and see some source files and headers doesn't include any required header (as programmer's class) or the standard ones (vector, string) , (even compiles perfectly).

One of these projects are Ogitor (an Ogre Scene Editor) i see their source and doesn't include some essential headers files, i see only the class definitions and very few forward class declarations, i don't make any change and compiles perfectly too!.

I want to get an approach on how to C/C++ developers achieve this, i don't want get a mess with a lot of #include directives, also giving me of incomplete class definition errors, i heard something of "precompiled headers" has something to do this.

¿Fue útil?

Solución

Precompiled headers are meant to speed up compilation time by being, well... precompiled. They may, but don't have to include other headers, and when they do, then you can skip their inclusion in the file that includes the precompiled header.

The reason you're not seeing all essential headers being included in every file which uses them is because they are indirectly included through other headers. There is no other way of making a header file accessible in a compilation unit, whether you use precompiled headers or not.

Example:

header1.h

#include <vector>
#include <string>    

header2.h

#include "header1.h"
#include <array>

header3.h

#include "header2.h"

std::vector<std::string> from_header1;
std::array<int> from_header2;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top