views:

60

answers:

2

So I'm chugging along in learning C++ and I'm starting to use Eclipse. As I create my .h files, I get this strange #define constant at the top:

#ifndef CLASSNAME_H_
#define CLASSNAME_H_
#endif /* CLASSNAME_H_ */

So, what gives? Am I supposed to use CLASSNAME_H_ for something?

(I should note that "classname" is just a filler. So, for example, my latest class was Person.h and I now have PERSON_H_)

+5  A: 

This is a standard construct used to guard against re-inclusion of your header files, I think you are probably expected to rename CLASSNAME_H_ to something more unique.

or is your header file also called classname.h?

Edit: ok so I see now that classname wasn't the actual value, but rather an example.

In that case, NO, you shouldn't need to worry about this at all just ignore them and they will do their job.

John Knoeller
they are refered to as "Include Guards" http://en.wikipedia.org/wiki/Include_guardOther compilers might have something like `#pragma once` to do the same thing
cmw
sorry, i updated my post. classname prolly wasn't the best choice for a filler :) .
Stephano
@cmw well done. i couldn't find this one in wikipedia to save my life. Thx!
Stephano
+1  A: 

Its just there to ensure that when that file is included multiple times during compilation that the contents aren't multiply defined. You don't use the CLASSNAME_H for anything, it just bookends the contents of that file.

Tony Albrecht