Sign in to follow this  
songofuriel

Use of #ifndef followede by #define

Recommended Posts

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 this post


Link to post
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 this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 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