typedef int: adding operators? (opAdd, etc.)

Charles D Hixson charleshixsn at earthlink.net
Tue Apr 3 22:12:41 PDT 2007


Is there any way to add operators defined on a type which is 
defined off of the built-in types?

In particular, it would be nice, e.g., to be able to define 
types for byteNo, wordNo, etc., and be able to add them 
together (where, say, one word == 4 bytes).

Currently I'm doing this kind of thing on an ad hoc basis, but 
that means that type information is spread out all over the 
place, and it would be nice to concentrate it in one place. 
(In one instance I'm converting between record ID's and disk 
addresses.  It's easy enough to do, but if I change the record 
size I'll need to track down each place where I cast from one 
type to another.  Not optimal.)

I suppose I could just write a special function for each 
conversion, and stick them all in the same place, and I may do 
just that, but this approach strikes me as less than ideal.

I suppose defining:
ByteNo opAdd(ByteNo b, WordNo w) { ... }
and then calling it via:
ByteNo b1 = opAdd(b0, w0);
isn't horrendous, but it's not as nice as
ByteNo b1 = b0 + w0;


More information about the Digitalmars-d-learn mailing list