Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?
From: SaltPeter (SaltPeter_at_Jupiter.sys)
Date: 04/10/04
- Next message: Bo Persson: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- Previous message: Rob Vermeulen: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- In reply to: Rob Vermeulen: "What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- Next in thread: Bo Persson: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- Reply: Bo Persson: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 10 Apr 2004 00:33:58 -0400
"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. 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.
- Next message: Bo Persson: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- Previous message: Rob Vermeulen: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- In reply to: Rob Vermeulen: "What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- Next in thread: Bo Persson: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- Reply: Bo Persson: "Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|