header files

Started by
10 comments, last by minamur 18 years, 2 months ago
I need some help. When you have a c++ project, you would have the main project file (.cpp) and a main header file (.h). All of the classes in the project would be defined in a header file (classname.h) and all of the workings of the class is in a classname.cpp. In some example programs i've looked at, the header files look like this: //file: classname.h #ifndef CLASSNAME_H #define CLASSNAME_H class ClassName { }; #endif //file: classname.cpp #include "classname.h" My question is, is this the correct structure that I should be following in my own programs? Also, what does #pragma once do? I've seen it alot, usually after the #define CLASSNAME_H line in the code above? Thanks.
Advertisement
You are using the correct structure and you will have a lot more class's in your projects as you program more complex things.

#pragma once does the same as #ifndef BLA_H
#define BLA_H

code

#endif

hope i helped
Quote:Original post by cNoob
You are using the correct structure and you will have a lot more class's in your projects as you program more complex things.

#pragma once does the same as #ifndef BLA_H
#define BLA_H

code

#endif

hope i helped


Only on compilers which support "pragma once". Not all compilers do. You can use pragma once, but you really should use the include guards as well.
Thanks to both of you. :)
Quote:Original post by rip-off
Quote:Original post by cNoob
You are using the correct structure and you will have a lot more class's in your projects as you program more complex things.

#pragma once does the same as #ifndef BLA_H
#define BLA_H

code

#endif

hope i helped


Only on compilers which support "pragma once". Not all compilers do. You can use pragma once, but you really should use the include guards as well.


Compilers_Which == Microsoft_Visual_C_Plus_Plus.

Yeah, it's correct.
To correct cNoob's example:

#pragma once#ifndef FOO_H#define FOO_H    //...code...#endif

will do what you expect.

As rip-off says, not all compilers support 'pragma once'. If one does, it marks the file as to be included and processed only once.
Otherwise, the preprocessor will skip the 'pragma once' line and process the include guards. Subsequently, the file will evaluate to a blank file in the preprocessor, which is slower than knowing beforehand to skip the file.

The code above is the most compatible way of using pragma once and include guards together. It will be faster than just include guards on compilers that support 'pragma once', but no slower on those that don't.

Hope that helps.
Quote:Compilers_Which == Microsoft_Visual_C_Plus_Plus.


Dev-C++ also allows #pragma once
DEV-C++ != a Compiler
it uses gcc/g++ as its compiler so if devc++ "supports" #pragma once
then you also could use it with other dists using gcc/g++...
A Programmer is Just a Tool which converts Coffeine into Code.
agi_shi: That signature is HUGE, cut it down a bit. It's currently taking up slightly more room than your actual post, and your post has a reasonable sized quote-block in it.

- Jason Astle-Adams

Quote:Original post by Kazgoroth
agi_shi: That signature is HUGE, cut it down a bit. It's currently taking up slightly more room than your actual post, and your post has a reasonable sized quote-block in it.


No problem. I'll do it right now.

This topic is closed to new replies.

Advertisement