Another C++ Question

LeetViet

Platinum Member
Mar 6, 2003
2,411
0
76
What is this line of code trying to do:

variable = something ? something->somethingelse() : 0;

Sorry, if I'm being vague. I don't know what the ? and : 0 are for.
 

akubi

Diamond Member
Apr 19, 2005
4,392
1
0
if something is true something->somethingelse() is assigned to variable, otherwise 0 is assigned to it.
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
Originally posted by: LeetViet
What is this line of code trying to do:

variable = something ? something->somethingelse() : 0;

Sorry, if I'm being vague. I don't know what the ? and : 0 are for.

If something isn't null the variable is the result of somethingelse() call on the ojbect pointed to by something. Otherwise it's 0.
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Originally posted by: LeetViet
What is this line of code trying to do:

variable = something ? something->somethingelse() : 0;

Sorry, if I'm being vague. I don't know what the ? and : 0 are for.

exact same thing as:

if(something)
variable = something->somethingelse();
else
variable = 0;

the ? operator is in a sense a short-hand for an if expression.
It's syntax is something like this:
( condition ? expression : expression)
and can be used anywhere an expression (like (1 + 2) or (x * ( y + z)) ) is required.
Here's how max can be defined w/ ? operator:
int max(int x, int y){
return (x > y) ? x : y;
}
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
As others stated it is a shorthand notation for a if/then/else statement.

Can make it a pain to read the code and does not make the execution of the code any faster if a decent compiler is used.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: dornick
I prefer the tertiary operator. It looks more compact.

Try to maintain cute code:thumbsdown:

 

oog

Golden Member
Feb 14, 2002
1,721
0
0
I've always called them ternary operators. And I don't think they make the code less readable. The g++ extension that allows self-assignment takes more getting used to. I wouldn't bother using these since they aren't in the standard:

int a = 5, b=4;
a <?= b; // a is assigned the value of b
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Originally posted by: oog
I've always called them ternary operators. And I don't think they make the code less readable. The g++ extension that allows self-assignment takes more getting used to. I wouldn't bother using these since they aren't in the standard:

int a = 5, b=4;
a <?= b; // a is assigned the value of b

what???? how is that different to a = b?
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
Originally posted by: statik213
Originally posted by: oog
I've always called them ternary operators. And I don't think they make the code less readable. The g++ extension that allows self-assignment takes more getting used to. I wouldn't bother using these since they aren't in the standard:

int a = 5, b=4;
a <?= b; // a is assigned the value of b

what???? how is that different to a = b?

I wouldn't worry about it too much. It's not in the C++ standard, and I think they were talking about rejecting it from being up into the standard. It's different from assignment because it's a conditional assignment. If a were less than b then the assignment wouldnt' happen. It's the equivalent of this:

a = ((a<b) ? a : b);

or

if (a<b) a=b;
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: EagleKeeper
As others stated it is a shorthand notation for a if/then/else statement.

Can make it a pain to read the code and does not make the execution of the code any faster if a decent compiler is used.

Anything is a pain to read if you're not used to it. There is nothing inherently hard to read about the ternary operator; in fact I value conciseness a lot, and for that reason I like it.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: BingBongWongFooey
Originally posted by: EagleKeeper
As others stated it is a shorthand notation for a if/then/else statement.

Can make it a pain to read the code and does not make the execution of the code any faster if a decent compiler is used.

Anything is a pain to read if you're not used to it. There is nothing inherently hard to read about the ternary operator; in fact I value conciseness a lot, and for that reason I like it.

Maintainence and documenation need to be considered when writing code.
to many people try and keep code as terse as possible and not indicate reason for logic.

These type of statement unless documented can become a both for someone attempting to maintain the code at a later date.

It can create poor habits down the road, even if the code itself is quality.

 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: EagleKeeper
Originally posted by: BingBongWongFooey
Originally posted by: EagleKeeper
As others stated it is a shorthand notation for a if/then/else statement.

Can make it a pain to read the code and does not make the execution of the code any faster if a decent compiler is used.

Anything is a pain to read if you're not used to it. There is nothing inherently hard to read about the ternary operator; in fact I value conciseness a lot, and for that reason I like it.

Maintainence and documenation need to be considered when writing code.
to many people try and keep code as terse as possible and not indicate reason for logic.

These type of statement unless documented can become a both for someone attempting to maintain the code at a later date.

It can create poor habits down the road, even if the code itself is quality.

Not indicating "reason for logic" (comments, descriptive names, etc) is an entirely separate issue from using the ternary operator.

if(something)
foo = bar;
else
foo = baz;

foo = something ? bar : baz;

There's nothing that makes the second harder to understand, except an individual's lack of familiarity with the syntax -- and anyone that doesn't understand the ternary operator shouldn't be working on any remotely serious C++ project.
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,003
126
I prefer the tertiary operator. It looks more compact.
Same here - I like to write as little code as possible and keep things compact. I hate scrolling through pages and pages of redundant code.