// you’re reading...

C++ Glossary

Function Overloading

Function polymorphism is a neat C++ addition to C’s capabilities

  • Whereas default arguments
    • Let you call the same function by using varying numbers of arguments
  • function polymorphism, also called function overloading
    • Lets you use multiple functions sharing the same name



The word 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



Both expressions boil down to the same thing,

  • But we’ll usually use the expression function overloading —it sounds harder working



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

  • For example, Mr. Computerson can root at the ball park for the home team, and he can root in soil for truffles
    • The context (one hopes) tells you which meaning of root is intended in each case
      • Similarly, C++ uses the context to decide which version of an overloaded function is intended



The key to function overloading

  • Is 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 (A-1):
      • When you then use a print() function, the compiler matches your use to the prototype that has the same signature (A-2):
        • For example, print(“Computers”, 15) uses a string and an integer as arguments, and it matches Prototype #1



Example of (A-1)

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



Example of (A-2)

print("Computers", 15);	// use #1
print("Keyboard");	         // use #5
print(2009.0, 10);	         // use #2
print(2009, 12);		// use #4
print(2009L, 15);		// use #3



When you use overloaded functions, you need to be sure you use the proper argument types in the function call

  • For example, consider the following statements (A-3):
    • Which prototype does the print() call match in (A-1)? It doesn’t match any of them!



Example of (A-3)

unsigned int year = 3210;
print(year, 6); // ambiguous call



A lack of a matching prototype doesn’t automatically rule out using one of the functions because C++ will try to use standard type conversions to force a match

  • If, say, the only print() prototype were #2
    • The function call print(year, 6) would convert the year value to type double
  • But in the earlier code (A-1) there are three prototypes that take a number as the first argument, providing three different choices for converting year
    • Faced with this ambiguous situation, C++ rejects the function call as an error



Some signatures that appear to be different from each other nonetheless can’t coexist

  • For example, consider these two prototypes (A-4):
    • You might think this is a place you could use function overloading because the function signatures appear to be different
  • But consider things from the compiler’s standpoint
    • Suppose you have code like this (A-5):
      • The x argument matches both the double x prototype and the double &x prototype
        • The compiler has no way of knowing which function to use
    • Therefore, to avoid such confusion
      • When it checks function signatures, the compiler considers a reference to a type and the type itself to be the same signature



Example of (A-4)

double cube(double x);
double cube(double & x);



Example of (A-5)

cout << cube(x);



The function-matching process does discriminate between const and non-const variables

  • Consider the following prototypes (A-6):
    • Here’s what various function calls would match (A-7):
  • The dribble() function has two prototypes—one for const pointers and one for regular pointers—and the compiler selects one or the other, depending on whether the actual argument is const
  • The dabble() function only matches a call with a non-const argument, but the drivel() function matches calls with either const or non-const arguments
    • The reason for this difference in behavior between drivel() and dabble() is that it’s valid to assign a non-const value to a const variable, but not vice versa



Example of (A-6)

void dribble(char * bits);		// overloaded
void dribble(const char *cbits);	// overloaded
void dabble(char * bits);		// not overloaded
void drivel(const char * bits);	// not overloaded



Example of (A-7)

const char p1[20] = "How's the weather?";
char p2[20] = "How's business?";
dribble(p1);	// dribble(const char *);
dribble(p2);	// dribble(char *);
dabble(p1);	// no match
dabble(p2);	// dabble(char *);
drivel(p1);	// drivel(const char *);
drivel(p2);	// drivel(const char *);



Keep in mind that the signature, not the function type, enables function overloading

  • For example, the following two declarations are incompatible (A-8):
    • Therefore, C++ doesn’t permit you to overload gronk() in this fashion
  • You can have different return types, but only if the signatures are also different (A-9):



Example of (A-8)

long gronk(int n, float m);	   // same signatures,
double gronk(int n, float m); // hence not allowed



Example of (A-9)

long gronk(int n, float m);	     // different signatures,
double gronk(float n, float m); // hence allowed



After we discuss templates later in other page

  • We’ll further discuss function matching


Discussion

No comments for “Function Overloading”

Post a comment