views:

54

answers:

1

Hello, This is quite probably a very silly question but I need to be sure. I've been given a class declaration in a header file eg.

#ifndef file_H
#define file_H

class ex{
private:

public:
};

#endif

and I've been required to write the method definitions in the same file, which I have done, my question is does the "#endif" stay where it is just after the class declaration or does it go at the end of my file after the class method definitions?.

+4  A: 

At the end of the file.

The goal of this form of this $ifndef pattern is to prevent a situation where the same declaration or definition appears twice in a compilation unit.

This is done because a C file may include several H files, which somewhere up the chain may in turn include the same file. IF you simply ran the preprocessor without these, you would have multiple copies of the H file. This way, you have multiple copies, but the preprocessor ignores everything after the first encounter.

Since you're not supposed to be defining anything more than once, if you have to place method definitions in the header file, put them within the #endif.

Uri
thank you very much
sil3nt