944,117 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 405
  • C++ RSS
Nov 10th, 2009
0

Double conditional on a pointer

Expand Post »
If I have the following setup:

C++ Syntax (Toggle Plain Text)
  1.  
  2. Point* MyPoint = Object->GetMyPoint();
  3.  
  4. if(MyPoint->GetValue() != 2)
  5. do something;
  6. else
  7. do something else;

If MyPoint is NULL or invalid, the MyPoint->GetValue() will cause a segfault.

This should fix it:
C++ Syntax (Toggle Plain Text)
  1.  
  2. Point* MyPoint = Object->GetMyPoint();
  3. if(MyPoint)
  4. {
  5. if(MyPoint->GetValue() != 2)
  6. do A;
  7. else
  8. do B;
  9. }
  10. else
  11. do B;

But that is quite awkward, as it makes me repeat B and it adds an extra nested layer.

Is there a better way to do this?

Thanks,

Dave
Similar Threads
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Nov 10th, 2009
0
Re: Double conditional on a pointer
Click to Expand / Collapse  Quote originally posted by daviddoria ...
If I have the following setup:

C++ Syntax (Toggle Plain Text)
  1.  
  2. Point* MyPoint = Object->GetMyPoint();
  3.  
  4. if(MyPoint->GetValue() != 2)
  5. do something;
  6. else
  7. do something else;

If MyPoint is NULL or invalid, the MyPoint->GetValue() will cause a segfault.

This should fix it:
C++ Syntax (Toggle Plain Text)
  1.  
  2. Point* MyPoint = Object->GetMyPoint();
  3. if(MyPoint)
  4. {
  5. if(MyPoint->GetValue() != 2)
  6. do A;
  7. else
  8. do B;
  9. }
  10. else
  11. do B;

But that is quite awkward, as it makes me repeat B and it adds an extra nested layer.

Is there a better way to do this?

Thanks,

Dave
This would be the simplest way of doing it:
C++ Syntax (Toggle Plain Text)
  1. Point* MyPoint = Object->GetMyPoint();
  2. if(MyPoint && MyPoint->GetValue() != 2)
  3. do A;
  4. else
  5. do B;

Cheers for now,
Jas.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Nov 10th, 2009
0
Re: Double conditional on a pointer
Jas, but won't it still crash evaluating the second part of the conditional?

Dave
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Nov 10th, 2009
0
Re: Double conditional on a pointer
If memory serves, if the left hand condition fails it won't bother evaluating the right hand one.
(depending on how the AND is evaluated...I think it gets evaluated left to right, so I'm pretty certain that if the first condition fails, it will ignore the 2nd condition and just return false! However if it is evaluated right to left, then you could simply swap the conditions over!)

Alternatively if your code is in a function you could do this:
C++ Syntax (Toggle Plain Text)
  1. Point* MyPoint = Object->GetMyPoint();
  2.  
  3. // if pointer is not valid, return...
  4. if(!MyPoint)
  5. return with an error condition/value;
  6.  
  7. // if we get this far, the pointer is valid
  8. if(MyPoint->GetValue() != 2)
  9. call function A;
  10. else
  11. call function B;
  12.  
  13. return success condition/value;

Cheers for now,
Jas.
Last edited by JasonHippy; Nov 10th, 2009 at 10:22 am. Reason: smelling pistakes and tpyos
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Nov 10th, 2009
0
Re: Double conditional on a pointer
Yup, as suspected; logical AND is evaluated left to right, so if the 1st condition fails / is false, then the 2nd condition is ignored and the logical AND operation returns false. The 2nd condition is only evaluated if the 1st condition is true.

So my original post should be fine!

Cheers for now,
Jas.
Last edited by JasonHippy; Nov 10th, 2009 at 10:31 am.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to capture a string into variable in a recursive function in C++?
Next Thread in C++ Forum Timeline: guys can you help me with this??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC