This documentation is archived and is not being maintained.

Conversions and Constants

Although constants for built-in types such as int, long, and double can appear in expressions, no constants of class types are allowed (this is partly because classes usually describe an object complicated enough to make notation inconvenient). However, if conversion constructors from built-in types are supplied, constants of these built-in types can be used in expressions, and the conversions cause correct behavior. For example, a Money class can have conversions from types long and double:

// spec1_conversions_and_constants.cpp
class Money
{
public:
    Money( long );
    Money( double );
   // ...
    Money operator+( const Money& );  // Overloaded addition operator.
};

int main()
{
}

Therefore, expressions such as the following can specify constant values:

Money AccountBalance = 37.89;
Money NewBalance = AccountBalance + 14L;

The second example involves the use of an overloaded addition operator, which is covered in the next section. Both examples cause the compiler to convert the constants to type Money before using them in the expressions.

See Also

Conversion Constructors

Show: