| | |
Double conditional on a pointer
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 634
Reputation:
Solved Threads: 46
If I have the following setup:
If MyPoint is NULL or invalid, the MyPoint->GetValue() will cause a segfault.
This should fix it:
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
C++ Syntax (Toggle Plain Text)
Point* MyPoint = Object->GetMyPoint(); if(MyPoint->GetValue() != 2) do something; else 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)
Point* MyPoint = Object->GetMyPoint(); if(MyPoint) { if(MyPoint->GetValue() != 2) do A; else do B; } else 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
0
#2 28 Days Ago
•
•
•
•
If I have the following setup:
C++ Syntax (Toggle Plain Text)
Point* MyPoint = Object->GetMyPoint(); if(MyPoint->GetValue() != 2) do something; else 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)
Point* MyPoint = Object->GetMyPoint(); if(MyPoint) { if(MyPoint->GetValue() != 2) do A; else do B; } else 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
C++ Syntax (Toggle Plain Text)
Point* MyPoint = Object->GetMyPoint(); if(MyPoint && MyPoint->GetValue() != 2) do A; else do B;
Cheers for now,
Jas.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Those who understand binary .....
And those who don't!
0
#4 28 Days Ago
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:
Cheers for now,
Jas.
(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)
Point* MyPoint = Object->GetMyPoint(); // if pointer is not valid, return... if(!MyPoint) return with an error condition/value; // if we get this far, the pointer is valid if(MyPoint->GetValue() != 2) call function A; else call function B; return success condition/value;
Cheers for now,
Jas.
Last edited by JasonHippy; 28 Days Ago at 10:22 am. Reason: smelling pistakes and tpyos
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Those who understand binary .....
And those who don't!
0
#5 28 Days Ago
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.
So my original post should be fine!
Cheers for now,
Jas.
Last edited by JasonHippy; 28 Days Ago at 10:31 am.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Those who understand binary .....
And those who don't!
![]() |
Similar Threads
- Insertion Into a Doubly Linked List (C++)
- any articles or books about pointer? (C++)
- pointer passing problem (C)
- Templates questions (C++)
- Doubts about References (C++)
- arrays and functionality of the program... (C++)
- Passing a matrix from main function to user defined function. (C++)
- why is double data type giving an garbage value (Java)
- How do I use a pointer variable and pointer arithmatic to display elements of array (C)
- Need help figuring out C++ Pointers (C++)
Other Threads in the C++ Forum
- Previous Thread: Test your C/C++ Skills [Free Online Test]
- Next Thread: guys can you help me with this??
| Thread Tools | Search this Thread |
api application array arrays beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





