Is it safe to test for a class member like this?

S

Steve

Hi,

if(myClass == nil || myClass.member == nil)
return;


Can I be sure that if my class == nil, that the next condition won't
even be tested and cause an error?

Thanks

Steve
 
S

Saeed Amrollahi

Hi,

if(myClass == nil  || myClass.member == nil)
    return;

Can I be sure that if my class == nil, that the next condition won't
even be tested  and cause an error?

Thanks

Steve

Hi Steve
According to Draft International Standard (N3092) 5.15(1):
the second operand [of logical OR operator] is not evaluated if
the first operand evaluates to true.
Same applies to && (logical AND operator). If the first operand
evaluates to false, the second operand isn't evaluated.
There are a few things in your code:
1. There's no "nil" in C++, May be you mean NULL,
or nil is a typedef of NULL?
2. In C++98 or C++03, you can use 0 (Zero) instead of NULL.
In C++0x, for pointers, you can use the pointer literal: 'nullptr'.
nullptr is a new keyword.

Regards,
-- Saeed Amrollahi
 
S

Steve

if(myClass == nil  || myClass.member == nil)
    return;
Can I be sure that if my class == nil, that the next condition won't
even be tested  and cause an error?

Steve

Hi Steve
According to Draft International Standard (N3092) 5.15(1):
the second operand [of logical OR operator] is not evaluated if
the first operand evaluates to true.
Same applies to && (logical AND operator). If the first operand
evaluates to false, the second operand isn't evaluated.
There are a few things in your code:
1. There's no "nil" in C++, May be you mean NULL,
   or nil is a typedef of NULL?
2. In C++98 or C++03, you can use 0 (Zero) instead of NULL.
   In C++0x, for pointers, you can use the pointer literal: 'nullptr'..
   nullptr is a new keyword.

Regards,
  -- Saeed Amrollahi

Thanks for the Standards info, Saeed. And yes, I should have written
NULL.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,370
Latest member
desertedtyro29

Latest Threads

Top