943,147 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 326
  • C++ RSS
Feb 21st, 2010
0

C++ Preprocessor

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. #ifndef Test_h
  2. #define Test_h
  3. #endif

May I know what are the above 3 syntax for? How can I use it?
Is is used only in header file?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
makan007 is offline Offline
70 posts
since Jan 2010
Feb 21st, 2010
0
Re: C++ Preprocessor
It's a guard against including the information in the header file more than once(which can cause a mess with redeclarations etc.).
You'd put the contents of your header between lines 2 and 3. At first pass if this symbol Test_h is not defined, processing goes to line 2 which defines Test_h and then continues with the rest of the header until the endif.
If you include this file again somewhere and that symbol is found to be already defined the preprocessor hits line 1 and skips to line 3 since the ifndef is false.
Last edited by jonsca; Feb 21st, 2010 at 6:10 am.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Feb 21st, 2010
0
Re: C++ Preprocessor
I can just use this right?

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
makan007 is offline Offline
70 posts
since Jan 2010
Feb 21st, 2010
0
Re: C++ Preprocessor
It's almost apples and oranges but not quite. Say you have a class A and you put the declaration for that class into it's own header file,one that you wrote. Say that your class B header file, B.h, includes A's header, A.h. If you then include A.h and B.h in main.cpp you will have 2 copies of A.h being included. Your guard prevents this and it's only included once. It doesn't seem like a complicated situation but with a large project there can be crossovers all over the place.

In the case of the headers that we normally include for library files (like iostream) they have their own guards in place to prevent this.
C++ Syntax (Toggle Plain Text)
  1. #ifndef _GLIBCXX_IOSTREAM
  2. #define _GLIBCXX_IOSTREAM 1
  3.  
  4. #pragma GCC system_header
is in the <iostream> header. I'm not sure if this particular pragma is involved in that whole process or not.

If you work with Visual Studio (and possibly other compilers) they also use the #pragma once directive to accomplish the same task.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How not to insert duplicate nodes in a bst
Next Thread in C++ Forum Timeline: MessageBox is putting out squares





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC