|
Ternary Operator ?:
The general form of the ternary operator is as follows:
expression1 ? expression2 : expression3 |
For the first operand - "expression1" - any expression that results in a bool type value can be used. If the result is true, then the operator set by the second operand, i.e. "expression2" is executed.
If the first operand is false, the third operand - "expression3" is performed. The second and third operands, i.e. "expression2" and "expression3" should return values of one type and should not be of void type. The result of the conditional operator execution is the result of expression2 or result of the expression3, depending on the result of expression1.
//--- normalize difference between open and close prices for a day range
|
This entry is equivalent to the following:
double true_range;
|
Based on the value of "expression1", the operator must return one of the two values - either "expression2" or "expression3". There are several limitations to these expressions:
Restrictions for the user-defined types (classes or structures):
Note
Be careful when using the conditional operator as an argument of an overloaded function, because the type of the result of a conditional operator is defined at the time of program compilation. And this type is determined as the larger of the types "expression2" and "expression3".
Example:
void func(double d) { Print("double argument: ",d); }
|
See also
Initialization of Variables, Visibility Scope and Lifetime of Variables, Creating and Deleting Objects