Click here to Skip to main content
15,890,123 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Can anyone see the problem-newbie Pin
Chris Losinger1-Jun-06 8:46
professionalChris Losinger1-Jun-06 8:46 
AnswerRe: Can anyone see the problem-newbie [modified] Pin
Zac Howland1-Jun-06 8:59
Zac Howland1-Jun-06 8:59 
AnswerRe: Can anyone see the problem-newbie [modified] Pin
David Crow1-Jun-06 9:19
David Crow1-Jun-06 9:19 
QuestionAnyone know of a good commercial debugger? Pin
leon91-Jun-06 7:01
leon91-Jun-06 7:01 
AnswerRe: Anyone know of a good commercial debugger? Pin
toxcct1-Jun-06 7:25
toxcct1-Jun-06 7:25 
GeneralRe: Anyone know of a good commercial debugger? Pin
BadKarma1-Jun-06 7:47
BadKarma1-Jun-06 7:47 
AnswerRe: Anyone know of a good commercial debugger? Pin
Blake Miller1-Jun-06 8:13
Blake Miller1-Jun-06 8:13 
AnswerRe: Anyone know of a good commercial debugger? Pin
leon91-Jun-06 8:49
leon91-Jun-06 8:49 
AnswerRe: Anyone know of a good commercial debugger? Pin
Zac Howland1-Jun-06 8:51
Zac Howland1-Jun-06 8:51 
AnswerRe: Anyone know of a good commercial debugger? Pin
Kevin McFarlane1-Jun-06 11:40
Kevin McFarlane1-Jun-06 11:40 
GeneralRe: Anyone know of a good commercial debugger? Pin
lastgen1-Jun-06 19:14
lastgen1-Jun-06 19:14 
GeneralRe: Anyone know of a good commercial debugger? Pin
Kevin McFarlane2-Jun-06 0:21
Kevin McFarlane2-Jun-06 0:21 
QuestionActiveX Control Debug version problem Pin
Andrew Hoole1-Jun-06 6:47
Andrew Hoole1-Jun-06 6:47 
AnswerRe: ActiveX Control Debug version problem Pin
Cedric Moonen1-Jun-06 7:21
Cedric Moonen1-Jun-06 7:21 
GeneralRe: ActiveX Control Debug version problem Pin
Andrew Hoole2-Jun-06 6:43
Andrew Hoole2-Jun-06 6:43 
Questionwave sound ... Pin
M_Nuaimi1-Jun-06 5:48
M_Nuaimi1-Jun-06 5:48 
AnswerRe: wave sound ... Pin
Zac Howland1-Jun-06 5:56
Zac Howland1-Jun-06 5:56 
QuestionRe: wave sound ... Pin
David Crow1-Jun-06 7:15
David Crow1-Jun-06 7:15 
QuestionWhy have they done this? Pin
RChin1-Jun-06 5:24
RChin1-Jun-06 5:24 
AnswerRe: Why have they done this? Pin
Viorel.1-Jun-06 5:34
Viorel.1-Jun-06 5:34 
GeneralRe: Why have they done this? Pin
RChin1-Jun-06 5:41
RChin1-Jun-06 5:41 
GeneralRe: Why have they done this? [modified] Pin
Zac Howland1-Jun-06 5:52
Zac Howland1-Jun-06 5:52 
The reason given in that article is actually pretty poor. Yes, it forces the programmer to use a trailing semi-colon, but that isn't necessary (mostly, they do that simply for looks). The real reason for enclosing it in a do-while loop is to prevent naming conflicts.

For example, lets say you have the following macro:
#define SWAP_INT(a, b) \<br />
   int c = a; \<br />
   a = b; \<br />
   b = c;


Now, your code looks something like the following:
void main()<br />
{<br />
   int a = 1, b = 2, c = 0;<br />
   SWAP_INT(a, b);   // compiler-error!  c is declared multiple times<br />
}


This creates a hard to find compiler-error that will annoy you (and anyone else trying to use that macro with a variable named 'c').

To prevent this, you could use the scoping operator:
#define SWAP_INT(a, b) \<br />
   { \<br />
   int c = a; \<br />
   a = b; \<br />
   b = c; \<br />
   }


However, some older C++ compilers do not treat this correctly (or even handle it at all. Thus, you can create a single loop do-while to handle the scoping:

#define SWAP_INT(a, b) \<br />
   do { \<br />
   int c = a; \<br />
   a = b; \<br />
   b = c; \<br />
   } while (0);


The trailing ; is purely optional. If you leave it off, you force the programmer to place it themselves (thus treating it like a function call). Putting it there doesn't hurt anything though.

As a side note, declaring it the way they did (as a macro) is poor programming practice. It really should have been declared as a function. If the user passes an int as the "logger", they will get some weird compiler error messages dealing with types and unless they search through the code to find the macro defintion, will have no idea why it is happening.

If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week

Zac

-- modified at 11:53 Thursday 1st June, 2006
GeneralRe: Why have they done this? [modified] Pin
RChin1-Jun-06 5:56
RChin1-Jun-06 5:56 
AnswerRe: Why have they done this? [modified] Pin
James R. Twine1-Jun-06 8:08
James R. Twine1-Jun-06 8:08 
GeneralRe: Why have they done this? [modified] Pin
Blake Miller1-Jun-06 8:18
Blake Miller1-Jun-06 8:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.