User-defined conversions?

Jeff McGlynn d at jeffrules.com
Tue Apr 3 16:22:33 PDT 2007


I'm in the process of translating my MySQL library from PHP to D.  When 
doing research I found that MySQL++ uses class operator overloading to 
automatically convert classes to basic types like so:

C++ ------------------
class convert {
	char * myVal;
	
	MyClass(char * inString) {
		myVal = inString;
	}
	
	operator int() {
		// Convert myVal to an int here ...
		return myValAsInt;
	}
	
	operator float() {
		// Convert myVal to float here ...
		return myValAsFloat;
	}
};

int someInt = convert("346");
float someFloat = convert("346.3");
-------------------------

Is there any way to do something similar in D?  The opCast function 
just attempts to cast whatever is returned to the proper type, which 
doesn't work for strings to ints.

-- 

Jeff McGlynn



More information about the Digitalmars-d-learn mailing list