C++: ? operator
I tried looking this up in Google and my C++ manual...but it''s not so easy to find with a search, obviously ...how does the ''?'' operator work?
The ternary operator is a (the) conditional expression.
e.g.
See also: This thread.
[edited by - Miserable on February 22, 2004 11:49:01 PM]
condition ? doiftrue : doiffalse
e.g.
int x = (a == 2)? 3 : 5;
See also: This thread.
[edited by - Miserable on February 22, 2004 11:49:01 PM]
It''s more of an alternative to using a if/else block for example (using the example above)
as you can see the ?: operator is a lot more concise an also allows you to achieve the equivalent of an if/else block on return statements:
if(a == 2){ x = 3;}else{ x = 5}
as you can see the ?: operator is a lot more concise an also allows you to achieve the equivalent of an if/else block on return statements:
bool SomeFunc(void){ int *Ptr = NULL; //blah return ((Ptr == NULL) ? false, true);}
quote:Original post by Spudderreturn ((Ptr == NULL) ? false, true);
That''s the perfect example on how to NOT use the ternary operator. This line should be:
return( Ptr != NULL );
The ternary operator is usually used as a shortcut to an if/else or switch block, if an assignment is based on a selection. For example:
*( isBoxFilled ? DrawFilledBox : DrawEmptyBox )(Coords, Color);
Instead of
if( isBoxFilled ) DrawFilledBox(Coords, Color);else DrawEmptyBox(Coords, Color);
Or perhaps:
color buttonColor = ( alert > 0 ) ? colorRed : colorGray;
It''s shorter, and more readable.
I wasn''t saying that was the best way to use the ?: operator, but it was the first example i could think of which demonstrated how ?: could represent an if/else block in a situation when you can''t actually use one which hopefully explained some of the benefits as when i was first starting out with C++ i couldn''t quite see the advantage of ?:
quote:Original post by Spudder
I wasn''t saying that was the best way to use the ?: operator, but it was the first example i could think of which demonstrated how ?: could represent an if/else block in a situation when you can''t actually use one which hopefully explained some of the benefits as when i was first starting out with C++ i couldn''t quite see the advantage of ?:
Well, the problem was not so much the use of ?: in itself, but the fact that your :? returned a boolean value. A ?: returning a bool simply doesn''t make sense, as the input of ?: is already a boolean.
These two:
bool b = a ? true : false;bool c = a ? false : true;
are totally equal to
bool b = a;bool c = !a;
A statement such as
return ((Ptr == NULL) ? false : true);
is valid C/C++, and will work, but it is redundant and meaningless. The ?: basically becomes a NO-OP, since the result is already available in the condition itself. Just as, for example
if( v == 2 ) v = 2;
So generally speaking, returning a bool from a ?: works fine, but it''s bad style. It won''t give any additional functionality, but make your code more difficult to read, and obfuscated.
A good place to use the ternary operator is in an intializer list of a class constructor:
--{You fight like a dairy farmer!}
class Happy{ Happy(int i, int j) : m_happy(i>5?5:i), m_joy(j<10?10:j) {} //...};
--{You fight like a dairy farmer!}
quote:Original post by ALX
Well, the problem was not so much the use of ?: in itself, but the fact that your :? returned a boolean value. A ?: returning a bool simply doesn''t make sense, as the input of ?: is already a boolean.
Plus, he used a comma (,) instead of a colon (...it shouldn''t even compile
Regards,
Jeff
[ CodeDread ]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement