ILE C/C++ Programmer's Guide

Porting ILE C Packed Decimal Data Types to the _DecimalT Class Template

In the class template _DecimalT, neither the constructor nor the assignment operator are overloaded to take any of the class template instantiations from _DecimalT. For this reason, explicit type casting that involves conversion from one _DecimalT class template object to another cannot be done directly. Instead, the macro __D must be used to embrace the expression that requires explicit type casting. This program in the following figure is written in ILE C:

Figure 224. ILE C Code that Uses Packed Decimal Data Types


#include <decimal.h>
 
void main ()
{
decimal(4,0) d40 = 123D;
decimal(6,0) d60 = d40;
d60 =  d40;
decimal(8,0) d80 = (decimal(7,0))1;
decimal(9,0) d90;
d60 = (decimal(7,0))12D;
d60 = (decimal(4,0))d80;
d60 = (decimal(4,0))(d80 + 1);
d60 = (decimal(4,0))(d80 + (float)4.500);
}

This source needs to be rewritten in ILE C++ as shown in the following figure:

Figure 225. ILE C++ Code that Uses the _DecimalT Class Template Instead of the C Packed Decimal Data Types



#include
int main ( void )
{ _DecimalT<4,0> d40 = __D("123"); // OK 
_DecimalT<6,0> d60 = __D(d40); // Because no constructor 
// exists that can convert d40 to d60. 
// macro __D is needed to convert d40 
// into an intermediate type first. 
_DecimalT<8,0> d80 = (_DecimalT<8,0>)1; // OK
// Type casting an int,not a decimal(n,p)
d60 = d40; // OK. This is different from the
// second statement in which
// the constructor was called.
// In this case, the assignment
// operator is called and the
// compiler converts d40 into the
// intermediate type automatically.
_DecimalT<9,0> d90; // OK
d60 = (_DecimalT<7,0>)__D("12"); // OK
d60 = (_DecimalT<4,0>)__D(d80);
d60 = (_DecimalT<4,0>)__D(d80 + 1);
d60 = (_DecimalT<4,0>)__D(d80 + (float)4.500);// In these three cases, the resultant
// classes of the expressions are
// _DecimalT<n,p>. macro __D is needed to
// convert them to an intermediate type
// first.
}


[ Top of Page | Previous Page | Next Page | Table of Contents ]