Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?
From: Bo Persson (bop_at_gmb.dk)
Date: 04/10/04
- Next message: Bo Persson: "Re: Interesting vector<> error in 6.0"
- Previous message: SaltPeter: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- In reply to: SaltPeter: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- Next in thread: SaltPeter: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- Reply: SaltPeter: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- Reply: Doug Harrison [MVP]: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 10 Apr 2004 11:54:31 +0200
"SaltPeter" <SaltPeter@Jupiter.sys> skrev i meddelandet
news:CMKdc.16559$BF2.1319444@news20.bellglobal.com...
>
> "Rob Vermeulen" <rvermeulen@arbor-audio-antispam-.com> wrote in message
> news:107dahl52n7ch3f@corp.supernews.com...
> > In several SDK examples of MS I notice the following.
> > To compare a pointer with NULL they do: "NULL == pMyPointer"
> > Why not just the other way around, like every "C++ for dummies" book
> > describes? :-)
> >
> > Is this some sort of optimisation ? (Can't imagine it is, actually,
since
> in
> > ASM it does not matter what operand you take first)
> > I was just curious if there is a sneaky reason why they do that.
> >
> > With regards,
> >
> > Rob.
> >
>
> As already mentioned, its a question of style. The primary reason is to
> announce your intentions to both the compiler and anyone reading your
code,
> including yourself. Note that if you wrote the following by accident:
>
> if(pMyPointer = NULL)
> {
> // whatever
> }
>
> The compiler won't detect the error in the code.
But it will!
Your code produces "warning C4706: assignment within conditional expression"
>And someone reading the
> code won't recognize without further inspection that your intention was to
> do a comparison.
>
> if(NULL == pMyPointer)
> {
> // whatever
> }
>
> makes the intention of the code crystal clear. Since NULL is a constant
only
> a comparison is allowed, no assignment possible.
The intention may be clear, but the code is ugly enough that people have to
ask news groups why the heck it is written this way. :-)
Not recommended.
Bo Persson
- Next message: Bo Persson: "Re: Interesting vector<> error in 6.0"
- Previous message: SaltPeter: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- In reply to: SaltPeter: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- Next in thread: SaltPeter: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- Reply: SaltPeter: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- Reply: Doug Harrison [MVP]: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|