Use of #ifndef followede by #define

Recommended Posts

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

Share on other sites
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.

Share on other sites
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

Share on other sites
can i define them as jpegfile_h instead of _h_jpegfile? and should it be in all caps? thanks :)

Share on other sites
You can use whatever you want but it must be unique, so the class/file name is a good choice

Share on other sites
Quote:
 Original post by songofurielcan 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.

Share on other sites
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

Share on other sites
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 :)

Share on other sites
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.

Share on other sites
alright. thank you very much, people. :)

Create an account

Register a new account

• Forum Statistics

• Total Topics
628293
• Total Posts
2981868

• 11
• 10
• 10
• 11
• 17