CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    some questions regarding header guards

    1. Which is the best way to do it?

    a.
    Code:
    #pragma once
    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++).

  2. #2
    Join Date
    Apr 2008
    Posts
    118

    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.

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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
  •  





Click Here to Expand Forum to Full Width

Featured