Saturday, December 26, 2015

Example 2: Demo Of Macro Functions.

     >>> Demo Of Macro Functions.


             #define Sqr(x)(x*x)
             #define Three (x)(x*x*x)

                      int a = 5, b = 0;


                      b = Sqr(a);
                      cout<<"\n\n Square = "<<b;

                      b = Three(a);
                      cout<<"\n\n Three = "<<b;


   >>> Macros suffer from four problems in C++> The First is that they can be confusing if they get large because all macros must be defined on one line. You can extend that line by using the backless character (\), but large macros quickly become difficult to manage.

   >>> This second problem is that macros are expanded in-line each time they are used. Thus means that if a macro is used a dozen times the substitution will appear 12 times in your program rather than appear once as a function call will.

   >>> On the other hand, they are usually quicker than a function call because the overhead of a function call is avoided.

   >>> The fact that they are expanded in-line leads to the third problem which is that the macro does not appear in the intermediate source code used by the compiler and therefore is unavailable in most debuggers.

No comments:

Post a Comment