Sign in to follow this  

header files

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
just the other day was the first time i've ever encountered a .hpp. figured it was just equivelent to .h for c++. so it isn't really a standard. maybe it's just a microsoft thing, since everytime you add a class to a project in visual studio it creates a .h and a .cpp, while the rest of the world has adopted the .hpp extension.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this