Function polymorphism is a neat C++ addition to C's capabilities. function polymorphism, also called function overloading, lets you use multiple functions sharing the same name.
polymorphism means having many forms.
So function polymorphism lets a function have many forms.
Similarly, the expression function overloading means you can attach more than one function to the same name, thus overloading the name.
You can use function overloading to design a family of functions that do essentially the same thing, but using different argument lists.
Overloaded functions are analogous to verbs having more than one meaning.
The key to function overloading it a function's argument list, also called the function signature.
If two functions use the same number and types of arguments in the same order, they have the same signature. the variable names don't matter.
C++ enables you to define two functions by the same name, provided that the functions have different signatures. The signature can differ in the number of arguments or in the type of arguments, or both. For example you can define a set of print() functions with the following prototypes:
void print(const char * str, int width); // #1
void print(double d, int width); // #2
void print(long l, int width); // #3
void print(int i, int width); // #4
void print(const char * str); // #5
When you use a print() function, the compiler matches your use to the prototype that has the same signature:
print("Pancakes", 15); // use #1
print("Syrup"); // use #5
print(1999.0, 10); // use #2
print(1999, 12); // use #4
print(1999L, 15); // use #3
When to use function overloading?
You might find function overloading fascinating, but you shouldn't overuse it. You should reserve function overloading for functions that perform basically the same task, but with different forms of data. Also you might want to check wether you can accomplish the same end by using default arguments.
char * left(const char * str, unsigned n); // two arguments
char * left(const char * str); // one argument
But using a single function with a default argument is simpler. There's just one function to write, instead of two, and the program requires memory for just one function, instead of two.
If you decide to modify the function, you have to edit only one. However, if you require different types of arguments, default arguments are of no avail, so in that case, you should use function overloading.
How does C++ keep track of which overloaded function is which?
It assigns a secret identity to each of these functions.
When you compile programs, the C++ compiler performs a bit of magic on your behalf--known as name decoration or name mangling--through which each function name is encrypted, based on the formal parameter types specified in the function's prototype. Consider the following undecorated function prototype:
long MyFunctionFoo(int, float);
For its own use, the compiler documents this interface by transforming the name into an internal representation with a more unsightly appearance, perhaps something like this:
?myFunctionFoo@@YAXH@Z
The apperent gibberish decorating the original name(or mangling is, depending on your attitude)
encodes the number and types of parameters. A different function signature would result in a different set of symbols being added, and different compilers would use different conventions for their efforts at decorating.
Abonneren op:
Reacties posten (Atom)
Geen opmerkingen:
Een reactie posten