Saturday, September 28, 2013

Which is better #defining / const and inline

Below material is wholly referenced from Effective C++ by Scott Meyer.
I think this might be useful for some one in urgent help and quick reading, and may be used as notes.. :)
If you have been a C Programmer you must be having a good habit of writing the constant values as:


#define PI 3.14

But how about writing the above line as

const double PI = 3.14

The second style of defining the variables , which will be same through the execution  is much more fruitful than the first  because of following reasons:
  • As first technique is just a preprocessor technique so PI will not  be available at the compile time , so if any error comes up relating to PI , it will be very hard to track it down
  • And the second one is, at the time of debugging you can’t see the value of  PI  , if its #defined  but it will be visible if defined as per second technique.
So because of above two main problems which may increase your coding time , it’s better to avoid such nuisances , adopt the second alternative Its much clearer and experienced C++ programmers do in that way.

In adopting with the second strategy ,below two points must be noted
  • If you are declaring  char pointer then do it in a safe way
    const char * const bookName = "C_PLUS_PLUS";
  • Second if you are using an Class Specific const , declare as its static member, But there is a trick in  in-class initializations ( static variables ) , which are initialized in the declaration of class header file.
    class Game
    {
    static const  int  MAX_PLAYERS = 10;
     
    int m_nArrayScores[MAX_PLAYERS];
    };
    
    
    Its much better to use the class specific consts which are pertaining to this class only, It’s not a good idea to mess up the global scope.    
    But this in-class initialization is valid only for integral types( ints , chars, bools ) but not for double or float
    For double and float static const variable u can only initialize  there variables in the class definiton file.
    class Game
    {
    // static const float  RATIO = 3.45;     //Error
    };
    The correct version of this would be 
    //At the time of declaration of class ( header file )
    class Game
    {
        static const float RATIO;
    };
    
    //At the time of definition in separate file
    const float GAME::RATIO = 3.45;
    The reason why double are not allowed in in-class initialization can be found at REASON WHY DOUBLE IS NOT ALLOWED in IN_CLASS INITIALIZATION
Another misuse of the Preprocessor directives is, we #define some of the basic functions
such as 
#define square(x) x*x

It has been taught that preprocessor macro arguments must be written enclosed in brackets as

#define square(x) (x)*(x)

I think this is most common problem in functions macros , why not we define an inline function , But if you talk about generic type ,, then templates can be used alongside with it as

template<typename T>
inline T square(T x)
{
return x*x;
}

Inline functions are much better and more runtime safe than macros.
 
So in my opinion if these techniques are adopted , our much of the time will be saved in resolving these issues.

No comments:

Post a Comment