Public Group

This topic is 4643 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by cNoobYou 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 #endifhope 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.

##### Share on other sites
Thanks to both of you. :)

##### Share on other sites
Quote:
Original post by rip-off
Quote:
 Original post by cNoobYou 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 #endifhope 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.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Compilers_Which == Microsoft_Visual_C_Plus_Plus.

Dev-C++ also allows #pragma once

##### Share on other sites
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++...

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by Kazgorothagi_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.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 11
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632922
• Total Posts
3009228
• ### Who's Online (See full list)

There are no registered users currently online

×