c++ help me organize my includes correctly

Started by
31 comments, last by popsoftheyear 12 years, 6 months ago

[quote name='pulpfist' timestamp='1318031148' post='4870312']
I think you should use either

#ifndef ?
#define ?
...
#endif

or

#pragma once

but not both,

Why not both? I use both all the time. GCC at one point didn't have good support for it, but now I believe having #pragma once and #ifndef checks gives you the best of both worlds for the vast majority of situations. More than likely unless you already know it will break your project having both, you will probably be fine using both.
[/quote]
I don't think there is a problem in using both but why use both when they do the same thing?
Advertisement
The usual explanation is that you get the performance benefit if the preprocessor supports pragma once, but it remains backwards compatible with tool chains that don't.
Just thought I'd chime in it is perfectly safe to declare a constant variable in a header. But you have to assign it something. No need for extern.


const unsigned int SOME_CONST = 23;


is perfectly okay in a header file, even if you include it a zillion times. The problem I saw above was the a constant variable was declared, but it wasn't assigned a value at the same place. Ie you were doing this:


const unsigned int SOME_CONST; // no good - how are you going to assign it now?

That would fix your problems with the const, assuming you have the appropriate inclusion guards. No need to use #define for the consts. As you noticed, using extern on the const globals created other problems when defining creating arrays because you had no way to know what the constant value actually is yet. No need to use extern for constant globals, just constant variables.

This topic is closed to new replies.

Advertisement