#2455 - animension - Tue Feb 04, 2003 9:45 am
Hi guys,
I'm trying to create a wrapper class for fixed point decimal operations such that numeric operations on the class would allow me to write code that closely resembles the use of floating point operations. I'll try to get down in writing exactly what it is I'm trying to do, but apologies beforehand if I end up sounding unclear.
I'd like to be able to specify the primitive type that the class wraps around in order to control storage requirements and memory overhead. Something like that is typically done with templates. The numeric operations on them would be a case of operator overloading. However, I'd also like to allow different instantiated class templates that have different primitive types the class wraps around interact with each other and with other primitives and constants.
What I'd like to see as an end result is being able to do something like the following:
Is there an elegant way to make the operator overloads take an ambigous Fixed<T> type (also primitive type) and add it to another Fixed<T> type and return an appropriate Fixed<T> type that is storing the resulting value?
I've tinkered around a bit with this idea but my implementations always end up having to declare argument types that are specific (Fixed<signed char> for example) for EACH operator overload, bloating my code. I'd imagine there's a way to do this by combining template classes, function templates, and operator overloads, but I can't seem to get the combination right.
If any C++ savvy person could point me in the right direction or heck, even recommend a book that extensively discusses the creation and use of class templates, function templates, etc, I'd be indebted.
Thanks!
_________________
"Beer is proof that God loves us and wants us to be happy."
-- Benjamin Franklin
I'm trying to create a wrapper class for fixed point decimal operations such that numeric operations on the class would allow me to write code that closely resembles the use of floating point operations. I'll try to get down in writing exactly what it is I'm trying to do, but apologies beforehand if I end up sounding unclear.
I'd like to be able to specify the primitive type that the class wraps around in order to control storage requirements and memory overhead. Something like that is typically done with templates. The numeric operations on them would be a case of operator overloading. However, I'd also like to allow different instantiated class templates that have different primitive types the class wraps around interact with each other and with other primitives and constants.
What I'd like to see as an end result is being able to do something like the following:
Code: |
Fixed<signed long int> fixedvar1 = 231.6521; Fixed<unsigned char> fixedvar2 = 0.923; unsigned short int var3 = 10; // adding an unsigned short to Fixed<signed long int> fixedvar += var3; // adding a primitive float constant to Fixed<signed long int> fixedvar += 22.528; // multiplying Fixed<signed long int> by Fixed<unsigned char> fixedvar *= fixedvar2; // adding primitive unsigned short int to Fixed<signed long int> and returning a Fixed<unsigned char> fixedvar2 = fixedvar1 + var3 |
Is there an elegant way to make the operator overloads take an ambigous Fixed<T> type (also primitive type) and add it to another Fixed<T> type and return an appropriate Fixed<T> type that is storing the resulting value?
I've tinkered around a bit with this idea but my implementations always end up having to declare argument types that are specific (Fixed<signed char> for example) for EACH operator overload, bloating my code. I'd imagine there's a way to do this by combining template classes, function templates, and operator overloads, but I can't seem to get the combination right.
If any C++ savvy person could point me in the right direction or heck, even recommend a book that extensively discusses the creation and use of class templates, function templates, etc, I'd be indebted.
Thanks!
_________________
"Beer is proof that God loves us and wants us to be happy."
-- Benjamin Franklin