|
-
March 8th, 2011, 05:23 AM
#1
some questions regarding header guards
1. Which is the best way to do it?
a.
b.
Code:
#ifndef THISFILESNAME_H
#define THISFILESNAME_H
//code in the file
#endif
?
Or, should I use both of them? Or, Is there a better way than these?
I currently use #pragma once (I'm using visual c++).
-
March 8th, 2011, 05:37 AM
#2
Re: some questions regarding header guards
The first method is not standard and will not work with all compilers; furthermore, when a pre-processor encounters a #pragma directive it does not understand, it is (usually) silently ignored, so you won't even know it has done nothing.
The second method is standard and will work.
-
March 8th, 2011, 05:44 AM
#3
Re: some questions regarding header guards
pragma once is not standard C++. It was developped by microsoft as an easy alternative to include guards. But, to be honest, if you are having trouble using include guards, its time to move on to another job.
Some people will tell you that pragma once can speed up compilation. May have been true 10 years ago.
I'd just stick to include guards. Everyone knows what they are, and how to use them.
EDIT: I'm not saying its wrong to use pragma once: It works perfectly well, and does it well. It's just I don't see the point of using something non-standard when you have a perfectly good standard way of doing it.
Last edited by monarch_dodra; March 8th, 2011 at 05:47 AM.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|