In ILE C, if both the second and third operands have an arithmetic type, the
usual arithmetic conversions are performed to bring them to a common
type.
In C++, both the second and third expressions must be of the same
class. If either expression has a different class, then you must cast
the second or third expression so that it has the same class.
The following figure illustrates an example where the conditional expression fails because the second and third expressions are not of the same class.
Figure 221. Example of a Conditional Expression that Fails because of Class Differences
#include <bcd.h> main() { int var_1; decimal(4,2) op_1_1 = __D("12.34"); decimal(10,2) op_1_2 = __D("123.45"); var_1 = (op_1_1 < op_1_2) ? (op_1_1 + 3) : op_1_2; } |
To use the conditional operator with the _DecimalT class template you can do either of the following:
The following figure shows an explicit cast on the second expression so that it has the same class as the third expression:
Figure 222. Example of an Explicit Cast that Resolves Class Differences between Expressions
#include <bcd.h> main() { int var_1; decimal(4,2) op_1_1 = __D("12.34"); decimal(10,2) op_1_2 = __D("123.45"); var_1 = (op_1_1 < op_1_2) ? (_DecimalT<10,2>)__D(op_1_1 + 3) : op_1_2; } |
The following figure shows how to use the same type of variables:
Figure 223. Example of Use of a Consistent Variable Type
#include <bcd.h> main() { int var_1; decimal(10,2) op_1_1 = __D("12.34"); decimal(10,2) op_1_2 = __D("123.45"); var_1 = (op_1_1 < op_1_2) ? op_1_1 : op_1_2; } |
(C) Copyright IBM Corporation 1992, 2005. All Rights Reserved.