Topic: Can casting operators for built-in types be overloaded?
Author: billd@cray.com (Bill Davidson)
Date: Wed, 18 May 1994 18:23:31 GMT Raw View
I'm fairly new to C++ and I've gotten impatient trying to read the
books so I've jumped to trying to write stuff and just look up what I
can't figure out.
I'm implementing a fixed point arithmetic class. I'd like it to act
exactly like any other arithmetic type. I seem to have it working
except that I'd like to be able to say things like:
double x;
fixed_point y = fixed_point(3.4);
x = double(y);
So far, I've been limited to calling the function for casting a
fixed_point number into a floating point number "real()" because I
can't figure out a syntax that the compiler will accept that lets me
use "double()".
Everything I can find on operator overloading says that you can
overload operators "blah blah and blah" and you can't overload
operators "blah blah and blah". The casting operator is never in
either list. I don't have a copy of the ARM.
Is this not possible or am I just doing something wrong?
Here's my class (fixed_point.h):
#ifndef INCLUDED_fixed_point_h
#define INCLUDED_fixed_point_h
#include "base_types.h"
/* FIXED_POINT_BITS *MUST* be an even number */
#define FIXED_POINT_BITS 16
#define FIXED_POINT_TYPE int32
#define FIXED_POINT_SCALE (1L<<FIXED_POINT_BITS)
class fixed_point {
FIXED_POINT_TYPE val;
public:
fixed_point();
fixed_point( int8 i );
fixed_point( int16 i );
fixed_point( int32 i );
fixed_point( float r );
fixed_point( double r );
friend fixed_point operator+(fixed_point, fixed_point);
friend fixed_point operator-(fixed_point, fixed_point);
friend fixed_point operator-(fixed_point);
friend fixed_point operator*(fixed_point, fixed_point);
friend fixed_point operator/(fixed_point, fixed_point);
friend FIXED_POINT_TYPE integer(fixed_point);
friend float real(fixed_point);
friend double real2(fixed_point);
};
fixed_point::fixed_point()
{
}
inline
fixed_point::fixed_point( int8 i )
{
val = (FIXED_POINT_TYPE)i << FIXED_POINT_BITS;
}
inline
fixed_point::fixed_point( int16 i )
{
val = (FIXED_POINT_TYPE)i << FIXED_POINT_BITS;
}
inline
fixed_point::fixed_point( int32 i )
{
val = (FIXED_POINT_TYPE)i << FIXED_POINT_BITS;
}
inline
fixed_point::fixed_point( float r )
{
val = (FIXED_POINT_TYPE)(r * FIXED_POINT_SCALE);
}
inline
fixed_point::fixed_point( double r )
{
val = (FIXED_POINT_TYPE)(r * FIXED_POINT_SCALE);
}
inline fixed_point
operator+( fixed_point a, fixed_point b )
{
fixed_point t;
t.val = a.val + b.val;
return t;
}
inline fixed_point
operator-( fixed_point a, fixed_point b )
{
fixed_point t;
t.val = a.val - b.val;
return t;
}
inline fixed_point
operator-( fixed_point a )
{
fixed_point t;
t.val = -a.val;
return t;
}
inline fixed_point
operator*( fixed_point a, fixed_point b )
{
fixed_point t;
t.val = (a.val >> (FIXED_POINT_BITS/2)) * (b.val >> (FIXED_POINT_BITS/2));
return t;
}
inline fixed_point
operator/( fixed_point a, fixed_point b )
{
fixed_point t;
t.val = (a.val << (FIXED_POINT_BITS/2)) / (b.val >> (FIXED_POINT_BITS/2));
return t;
}
inline FIXED_POINT_TYPE
integer( fixed_point a )
{
return a.val >> FIXED_POINT_BITS;
}
inline float
real( fixed_point a )
{
return (float)a.val / (float)FIXED_POINT_SCALE;
}
inline double
real2( fixed_point a )
{
return (double)a.val / (double)FIXED_POINT_SCALE;
}
#endif /* INCLUDED_fixed_point_h */