Use of #ifndef followede by #define

Started by
8 comments, last by songofuriel 16 years, 11 months ago
Hi, I just would like to ask the use of creating something like this in your program: http://img441.imageshack.us/img441/8681/q01hb7.jpg
Advertisement
Taken from someone else's explanation:

This usage, commonly called an "include guard", prevents the contents
in between the #ifndef and the #endif from being processed more than
once in any given source file. It will still be processed once only
for every individual source file in a program that includes it at
least once.

Note that proper usage for headers is that they should not define
actual variables or contain the bodies of any functions except for
inline member functions of classes. The definitions of type, such as
class definitions, and function prototypes do not in themselves
generate code or data.

--
Jack Klein


This kind of question is more appropriate for the General Programming board as its not specific to game programming. Its a general practice in C/C++ programming, so you'll have a better chance of getting proper information about stuff like this there.
Suppose for instance you have a header file that is included by multiple source files in your project. In this case, using a "ifndef/define" combo as you illustrate there can prevent multiple definition errors that would otherwise result.

For instance

#ifndef __MY_CLASS__#define __MY_CLASS__class {whatever here}#endif
can i define them as jpegfile_h instead of _h_jpegfile? and should it be in all caps? thanks :)
You can use whatever you want but it must be unique, so the class/file name is a good choice
Quote:Original post by songofuriel
can i define them as jpegfile_h instead of _h_jpegfile? and should it be in all caps? thanks :)


you can define them however you want, however you want to make sure that no other header uses the same definition as that could ruin things for you.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
In fact you SHOULD NOT choose identifiers starting with underscores, such identifiers are reserved by the standard for use by the compiler.

JPEGFILE_H is reasonably common and doesn't suffer from the same problem
You should avoid using names the begin with a leading underscore as the c++ standard reserves them for use by the implementation in the global and std namespaces.

Edit: beaten :)
Beaten, I'm sure, but I don't care:

You CAN define them as anything you like, as long as they are unique. To ensure uniqueness, you SHOULD name them in *some* way that corresponds to the file name.

You SHOULD name them in all uppercase, so they stand out and so their purpose is recognized immediately by other coders.

You SHOULD NOT use leading underscores, as in the original example! In particular, you MUST NOT use two leading underscores, nor a leading underscore followed by a capital letter. And since you will normally be putting the names in uppercase, this practically means not to use leading underscores at all.
alright. thank you very much, people. :)

This topic is closed to new replies.

Advertisement