View Single Post
Old Jun 24th, 2005, 10:06 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
These are definitions for my library functions (MS C++).
Note that for C++, the fabs and abs functions are already overloaded; that is, they may be called with either a float, a double, or a long double (fabs), or with an int, a long, a double, or a long double (abs). In C, one would have to use fabs for a double or fabsf for a float and abs for an int or _abs64 for an __int64 (MS definition).
int abs(int n);
long abs(long n);                    // C++ only
double abs(double n);              // C++ only
long double abs(long double n); // C++ only
float abs(float n);                   // C++ only
__int64 _abs64(__int64 n);
long labs (long n);
float fabs(float x);                  // C++ only
double fabs(double x);
long double fabs(long double x); // C++ only
float fabsf(float x);
One could write a single function which incorporated the appropriate member of this list, depending upon the arguments in the declaration/definition. Note that the return value is not a valid item in distinguishing among overloaded functions.

The same identifier may not be used for different entities simply because it introduces an ambiguity -- the compiler has no way to know which entity to use. Some seemingly same-named entities appear to be used because the ambiguity is resolved with scope (in this area of the program use this one; in that area, use that one). Overloading uses a different mechanism. The compiler deduces the appropriate entity by part of the context (the argument list). The entity is then renamed (behind the scenes) by the compiler in order to carry the uniqueness of the function through the layer of abstraction to the machine.

If I'm beating a horse you've already ridden, I apologize. I can't tell from your post precisely what your level of familiarity is.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote