5

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.

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?.

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.