Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

maxplus305

C++: ? operator

This topic is 5259 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
The ternary operator is a (the) conditional expression.
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]

Share this post


Link to post
Share on other sites
It''s more of an alternative to using a if/else block for example (using the example above)

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);
}


Share this post


Link to post
Share on other sites
quote:
Original post by Spudder

return ((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.

Share this post


Link to post
Share on other sites
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 ?:

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
A good place to use the ternary operator is in an intializer list of a class constructor:


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!}

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!