#ifs and assert()s

Hi there. I'm studying C++ out of C++ for dummies, 6th edition.

When it comes to #ifs, I get the general idea of what it's supposed to do, but I have trouble seeing what that could be used for. I'm on page 155 and I see some code snippets. and I'll put comments where I'm confused.

#if DEBUG == 1
inline void logMessage(const char *pMessage)
{ cout << pMessage << endl; }
#else

//It says this does nothing. Does that mean it doesn't just return 0?
#define logMessage(X) (0)
#endif

//At this point, there's a gap in the snippit where it says "I can now sprinkle
//error messages throughout my program whenever I need them:"
//Does it pick up here?

#define DEBUG 1
//This looks kind of superfluous to me. What exactly am I including here? A
//file, a function, or what? Note that it begins with upper case this time.
#include "LogMessage"
void testFunction(char *pArg)
{
logMessage(pArg);
//The next comment line is taken from the book:
// ...function continues...

So, can someone please clear this up for me?

Also, they introduced the assert(), but I have no idea where to put it or how it would be used if I did. I tried putting it in a function like:

int factorian(int N)
{
assert(N > 0);
//...program continues...


but it said that assert was not defined.

Remember, I only know what I know from the book up to page 160, so please don't get to technical on me. It's my first language and I've had ZERO experience programming before this.
#if is used for conditional assembly.

In your example, one of two definitions for logMessage will be compiled depending on the value of the preprocessor sybol DEBUG.

In the first case, a small inline function named logMessage is compiled.
If DEBUG is not 1, then a preprocessor macro called logMessage is defined.
Whenever logMessage occurs in the code, (0) is substituted. (0) is really a no-op (it doesn't do anything. A better definition would have been nothing:
 
#define logMessage(X) 


Apparently, the above #if code was placed in a file called "logmessage". The #include causes the contents of that file to be included.

In testFunction(), logMessage(pArg) will do one of two things, depending on which version of logMessage was defined. It will either print the passed argument or do nothing.

assert( cond ) is a macro that will test if the specified condition is false and if so, call abort() with an appropriate error message. You need a #include <cassert> to define the assert macro.
http://www.cplusplus.com/reference/cassert/assert/



Hey, thanks dude! That makes more sense now!

Now, it, talks about intrinsically defined constants.
_FILE_
_LINE_
_func_
_DATE_
_TIME_
_TIMESTAMP_
_STDC_
_cplusplus_

I tried putting cout << _TIMESTAMP_; but it said that it wasn't declared in this scope. How do I make it so it outputs the time stamp? And the other constants for that matter. Just to give me an idea of how they're used.
Intrinsically defined constants are constants that are defined by the compiler.
Some like _FILE_ and _LINE_ are standard. Others are implementation defined, so check your compiler's documentation for which ones your compiler supports.
I've never come across _TIMESTAMP_.

For example, you could change the logMessaage macro above to the following:
[code]
#if DEBUG == 1
#define logMessage(X) \
cout << X << " at line " << _LINE_ << " in file " << _FILE_ << endl;
#endif
[code]
So whenever you execute the logMessage macro in your code, it would display the argument, the source file and line number.
Topic archived. No new replies allowed.