[gnu.g++] User-Defined Type conversions: A Question

dsouza@mcc.com (02/09/90)

We would like to define some data types which essentially behave like
floats (or doubles, or ints ..) except that they have some
additional functionality. The obvious way to do this would seem to be
to define a class (like MyFloat, below) with the additional
functionality and define a type conversion operator from that class to
"float". 

BUT ....

Lippman (p 83) states:
"..in arithmetic expressions, the widest data type present becomes the
target conversion".

The AT&T Language Ref. Manual lists 6 rules (p 88) used for comparing
different matches for overloaded function calls. Based on these, we
have not been able to figure out how the example below should behave.

Would someone please explain what the behaviour of this example
program should be, and WHY ?

#include <stream.h>

class MyFloat {
public:
  float value;
  MyFloat() : value(3.6) {}
  operator double() { return value; }
  float operator =(float f) {value = f; return f;}
};


main() {
  MyFloat f;
  f = f + 3;
  cout << f.value << "\n"; // 
}


Question: WHAT should be printed out ? 6 ? 6.6 ?

g++ (1.36.4) compiles this and prints 6
SUN C++ compiles and prints 6.6.

We would like a way to make MyFloat behave like a float in such
contexts, i.e. print out 6.6. How do we do this ?


Thanks

-------------------------------------------------------------------------------
 Desmond D'Souza, MCC CAD Program | ARPA: dsouza@mcc.com | Phone: [512] 338-3324
 Box 200195, Austin, TX 78720 | UUCP: {uunet,harvard,gatech,pyramid}!cs.utexas.edu!milano!cadillac!dsouza