2 Overloading

C++ permits function and operator overloading. Overloading means the same function name, or operator, may have different definitions based different parameters and return types. The following definitions are permitted in C++, but not in C:

void f(void)
{
  // ...
}

void f(int i)
{
  // ...
}

void f(MyClass x)
{
  // ...
}

Overloading is possible because a C++ compiler can differentiate which function to use by context. In other words, a C++ compiler knows which actual subroutine to use in the following code:

{
  MyClass mine;

  f(mine);
}

Overloading comes in handy because we don't need to invent new names for the same abstract operation on different types. However, this convenience is not important compared to the possibility of using template functions and template classes.



Copyright © 2006-09-12 by Tak Auyeung