Conditional assignment ?:

Started by
12 comments, last by sevak 20 years, 3 months ago
I'll use it whenever it applies. I used it in one of my programs for a class I'm taking at school where I had to delete a certain number of characters and I wanted to make sure it was positive so it looked like delete(x > 0 ? x : 0) and my teacher had never seen the operator before. Kind of scarry.

Edit: I've never used it like ffx showed, I only have one conditional. In that case I'd just use if statements. So far I've only used it on the right hand of an assignment and when passing an argument to a function.

[edited by - Aerolithe on January 4, 2004 4:34:03 PM]
Advertisement
?: is useful in obnoxiously scoped code segments. For example, member initializer lists, initialization of const variables, initialization of references and inside of macros where the block expansion of if/else would bork up expression/statement distinctions.

In reasonably complex code, I''d be somewhat suprised if I didn''t see at least one initializer list with a ?: inside and very suprised if I didn''t see at least one macro used with a ?: inside.
quote:Original post by antareus
You realize thats a lot harder to read, right?
I thought he used it in a way that is very clear to read, clearer, less repetitive and simpler than the alternatives using switch/case or if/elses. Well, not everyone (e.g. you) shares my taste but you gotta see that this isn''t a situation of "realizing" something as you put it.
quote:Original post by Anonymous Poster
clear to read, clearer
Ew, maybe I should be less repetitive myself

This topic is closed to new replies.

Advertisement