Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Apr 23, 2001

Using MACRO Definitions


By using MACRO definitions, you can easily write lines of code that are only included in Debug versions and not in Release versions. For instance, if you create a function 'MyLog(x,y)' that writes debugging information to a file you can define a macro such as:
 
#ifdef DEBUG
#define LOG(a,b)   MyLog(a,b);
#else
#define LOG(a,b)
#endif

Using conditional compilation, you can remove the 'MyLog' code from your Release version. However, in case of logging, you want to pass a variable amount of parameters most of the time. In this case the macro definition mentioned above doesn't work. Nevertheless, there is a nice workaround. The solution is to redefine the macro as:
 
#ifdef DEBUG
#define LOG(a)   MyLog a;
#else
#define LOG(a)
#endif

In your code you should use it as:
 
…
LOG ((a,b,c))
…
LOG ((a,b,c,d,e))
…

By putting the parameters between double brackets, you can pass a variable amount of parameters to your macro.
Dominique Bijnens
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date