Solved

What does IFNDEF do and why do I care to use it?  Please explain.

Posted on 2011-02-12
6
566 Views
Last Modified: 2013-12-14
Please see code...

What is the point of IFNDEF?  

And FUNCTIONS_H_INCLUDED?

And define FUNCTIONS_H_INCLUDED?
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

class MathFunc
{
public:
     static int add(int operand1, int operand2);
};

#endif

Open in new window

0
Comment
Question by:shaolinfunk
  • 3
  • 2
6 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 34879787
The construct above is a so called "header guard" (http://en.wikipedia.org/wiki/Include_guard), which is used to ensure that a header file and the declarations it holds are only seen once by the compiler, thus avoiding "symbolmultiply defined" errors. See also http://faculty.cs.niu.edu/~mcmahon/CS241/c241man/node90.html ("Include guard")
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 250 total points
ID: 34879788
The # indicates something the compiler should be checking.
#ifndef   - if not defined
#ifdef    - if defined
both work with the #else and #endif, like the if...else... statements in code.

The FUNCTIONS_H_INCLUDED is just something (unique) you made up.


#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

class MathFunc
{
public:
     static int add(int operand1, int operand2);
};

#endif

Open in new window

basicaly that tells the compiler that if FUNCTIONS_H_INCLUDED has not yet been defined that it should enter this code section, assign a value to FUNCTIONS_H_INCLUDED (so it won't be entred again) then compile that block of code
0
 
LVL 86

Expert Comment

by:jkr
ID: 34879795
BTW, to elaborate on your code example:
#ifndef FUNCTIONS_H_INCLUDED // precompiler checks if that symbol has already been set. If so, the entire code up to '#endif' will be skipped
#define FUNCTIONS_H_INCLUDED // symbol not encountered earlier, define it. A second encounter will trigger the effect menioned above

class MathFunc
{
public:
     static int add(int operand1, int operand2);
};

#endif // Kinda the 'closing brace' for the conditional compilation

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 86

Expert Comment

by:jkr
ID: 34879800
And a final 'PS:' on the whole concept of conditional compilation: http://en.wikipedia.org/wiki/C_preprocessor#Conditional_compilation
0
 
LVL 1

Author Closing Comment

by:shaolinfunk
ID: 34879803
Very illuminating.  Thank you, kind sirs!
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34879806
Thank you for that additional post jkr.  That helped!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question