TITLE: overloading based on function return type (Newsgroups: comp.std.c++, 10 May 99) ADHIA: Paresh Adhia > e.g. wouldn't it be nice to overload like, > > String getQty(); > int getQty(); > double getQty(); NARAN: Siemel Naran In the absence of overloading by return value, the expressions "getQty()", "a+b", and "3/4" are well defined. Just look at the argument types and determine which function to call. For example, "3/4" calls operator/(int,int), and the answer is zero. But in the presence of overloading by return value, the same expressions are not well defined. We have to look at the type declaration to determine which function to call -- String s=getQty(); // call String getQty() int s=getQty(); // call int getQty() In other words, the meaning of an expression depends on the context in which it is used. Even if this sort of thing were possible, it would make program maintainance much more difficult. ADHIA: > The other alternative, I could think of was to provide type conversion > operators between various return types, but that approach is dangerous > as it will be applicable too generally. NARAN: Actually, I'll let you in on a secret. You can overload by return value in C++. struct getQty { operator int() const; operator String() const; }; And to use int i=getQty(); // calls getQty::operator int() const String s=getQty(); // calls getQty::operator String() const Technically, this is not overloading by return value. Practically, it is. But I think it will lead to code that is hard to maintain, so my recommendation is to stay away from it.