pete@abccam.abcl.co.uk (Peter Cockerell) (02/12/90)
Has anyone come across this bug in Zortech C++ 2.06, and is there a fixed version? class String { char *str; ... public: //Various constructors String(char*); String(S&); String(int); //Various type convs. operator char*() { return str; } operator int() { return *str != '\0'; }//ie return *this is non-null String& operator=(String&); ... }; ... main() { String c1="Y", c2="N"; String res; res = 1 ? c1 : c2; printf((char*)res); } This prints 1. The reason is not that the 1 from the conditional expression is being converted to a String, as you might at first guess, but because the second expression, c1, is for some reason automatically being cast to an int (using the operator int()) yielding 1, which is then being cast to a String for the assignment. (That's what it looks like from the code generation anyway.) If you say instead if (1) res=c1; else res=c2; you get Y printed, as you might expect. Any comments?