Octothorpial Commands

The octothorpe (called by normal people the pound sign) (#) is used in C++ for pre-processor directives. These commands will be executed prior to any compilation taking place.

These commands are useful when conditional compilation is required (for example when you want debugging code to be included only when you need it); and, they can be essential when decomposing C++ programs.

The conundrum that occurs during decomposition is that C++ is stupid. It does not know how to handle (as java does) multiple includes.  

Suppose, for example, that you have a_class.h and b_class.h each including c_class.h.  If d_class.cpp includes a_class.h and b_class.h, then the complier may complain about multiple definitions of c_class.

To prevent this, we use #ifndef, #define and #endif.

The way it works is that in c_class.h we put the code:

#ifndef x  //this name is unconventional,

           //but syntactically correct.

#define x

class c_class{int m;} // the actual code goes here


The first time

 #include "c_class.h"

is used, x will be defined.  Once it is defined, it will never be defined again.  This means that

#include "b_class.h"

#include "a_class.h"

in d_class.cpp will cause the definition of c_class to be done once and only once.


Tangentially,  to conditionally include debugging statements,

#define DEBUG // place at the top of the file

#ifdef DEBUG

   cout << " sanity check point 23 " << endl;


to suppress them

#define DEBUG

#ifndef DEBUG

   cout << " sanity check point 23 " << endl;