Sign in to follow this  
Bombario

Placing #Includes in an Included File

Recommended Posts

Bombario    109
When I do stuff I like to be as organized as possible. Easy navigation around a website, document, or anything else is a must. With my recent start of learning C++ (I've only had experience with BASIC before) some questions have popped up. Is it logical or even possible to "#include" a cpp or h file that only contains other includes? That way you could just say #include "includes.cpp", and add anything you need to includes.cpp, and your main source file wouldn't be all junked up at the top.

Share this post


Link to post
Share on other sites
pinacolada    834
Yup it's possible, although you would probably name it "includes.h" instead of "includes.cpp". In Visual Studio projects, they set up a thing like this for you, but they call it "stdafx.h". What "stdafx" actually stands for, I have no idea.

Share this post


Link to post
Share on other sites
Oluseyi    2116
Never include a .cpp file. While it's just a text file, your IDE interprets and handles it differently, meaning it will attempt to compile it. Much confusion will ensue.

There's nothing wrong with including a .h file in another .h file. Many development environment support something called precompiled headers, in which one .h file is created to include all the commonly needed headers, and a matching .cpp file is used to compile the declarations into object code ahead of time. You might want to look into it.

Share this post


Link to post
Share on other sites
hplus0603    11356
Yes, it is possible to #include inside other #includes. However, it's bad form to #include a .cpp files; all files intended to be #included should usually be named .h. .cpp files are typically reserved for translation units (that actually get compiled).

However, including from other includes is often a bad idea, because if you change any of those included files, everything that indirectly includes it will have to be re-compiled. When your project has thousands of .cpp files in it, re-compiling everything because you changed a single header is no fun.

The alternative is to use forward declarations as much as possible. Thus, if you have a function called Foo which takes a class CBar pointer, then you can declare it in two ways:

// bad
#include "Bar.h"
void Foo( Bar * );

// good
class Bar; // forward declaration
void Foo( Bar * );

Then, inside Foo.cpp, you have to #include "Bar.h", but that's better than implicitly forcing that #include on everyone who includes Foo.h.

A great book about this is "Large-scale C++ software design," I believe by a guy named Lankos.

Share this post


Link to post
Share on other sites
Oluseyi    2116
Quote:
Original post by pinacolada
In Visual Studio projects, they set up a thing like this for you, but they call it "stdafx.h". What "stdafx" actually stands for, I have no idea.
stdafx.h (and stdafx.cpp) is the pre-compiled header. "stdafx" breaks down as "standard afx"; MFC, WTL and a few other Microsoft frameworks use the "afx" prefix for a number of their internal structures and functions (these were created before the advent of namespaces).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this