Click here to Skip to main content
15,881,281 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
GeneralRe: Why have they done this? [modified] Pin
Zac Howland1-Jun-06 8:46
Zac Howland1-Jun-06 8:46 
AnswerRe: Why have they done this? Pin
toxcct1-Jun-06 5:46
toxcct1-Jun-06 5:46 
QuestionSDL window in MFC application. Pin
Manjunath S1-Jun-06 5:22
Manjunath S1-Jun-06 5:22 
QuestionRe: SDL window in MFC application. Pin
Hamid_RT1-Jun-06 5:51
Hamid_RT1-Jun-06 5:51 
AnswerRe: SDL window in MFC application. Pin
Cedric Moonen1-Jun-06 6:05
Cedric Moonen1-Jun-06 6:05 
GeneralRe: SDL window in MFC application. Pin
Hamid_RT1-Jun-06 6:10
Hamid_RT1-Jun-06 6:10 
GeneralRe: SDL window in MFC application. Pin
Cedric Moonen1-Jun-06 6:11
Cedric Moonen1-Jun-06 6:11 
GeneralRe: SDL window in MFC application. Pin
Hamid_RT1-Jun-06 20:02
Hamid_RT1-Jun-06 20:02 
AnswerRe: SDL window in MFC application. [modified] Pin
toxcct1-Jun-06 6:19
toxcct1-Jun-06 6:19 
GeneralRe: SDL window in MFC application. Pin
David Crow1-Jun-06 7:21
David Crow1-Jun-06 7:21 
GeneralRe: SDL window in MFC application. Pin
toxcct1-Jun-06 7:24
toxcct1-Jun-06 7:24 
GeneralRe: SDL window in MFC application. Pin
ThatsAlok1-Jun-06 19:12
ThatsAlok1-Jun-06 19:12 
QuestionCString/strcpy error Pin
kitty51-Jun-06 5:07
kitty51-Jun-06 5:07 
AnswerRe: CString/strcpy error [modified] Pin
Viorel.1-Jun-06 5:15
Viorel.1-Jun-06 5:15 

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.