Topic: Overloading automatic type conversion


Author: steve@taumet.com (Stephen D. Clamage)
Date: 15 Jul 91 15:29:26 GMT
Raw View
whatis@wookumz.gnu.ai.mit.edu (Steve Boswell) writes:

>I am writing a BigInt class (like everyone else when they're parcticing
>their OOP) and I wanted to know if there was any way to hook my class
>definition into the basic integer class hierarchy, the way Lisp will
>convert a fixnum to a bignum automatically when the values reach
>the largest fixnum value.

What you describe doesn't happen in C/C++ even with the built_in types.
The type of an expression depends on the types, not the values, of its
operands.  You may be thinking about the type of a literal integer
constant, whose type may depend on its value.

If you have the following:
 int i = INT_MAX - 1;
 ...(i+2)...
the type of (i+2) is int, and the value of that is not INT_MAX+1.

You can define your BinInt class with conversions to and from the
built-in integer types, and use casts where needed to avoid overflow,
exactly as you would with the built-in types.

--

Steve Clamage, TauMetric Corp, steve@taumet.com