C++ Concept Map

Overloading

The usual rules of scope forbid the re-use of a name in the same scope. In C++ this relaxed for function members which can have the same name as long as their signatures are different. Thus a class may have two functions called overload, one of which has no parameters, and one of which has a single integer parameter. Each function overloading must have its own unique definition, thus there really are multiple meanings for the same name, even within the same scope.

class OL {
...
public:
	void overload();
	void overload(int);
...
};

In general, a function may be overloaded by altering its signature, however, because of the implicit conversion of types in C, some overloadings are ambiguous. This is true of any function which only differs by its return type. e.g.

void F(int);

and

int F(int);

cannot be distinguished by the compiler given a call

F(10); 

An example where there is also ambiguity is:

void F(int);

and

void F(long);

given the same call

F(10); 

Again these two cannot be distinguished by the compiler due to implicit coercion among the numeric types.

 

Operators may also be overloaded, as can constructors.


Please mail any corrections and/or suggestions to Roger Hartley at
rth@cs.nmsu.edu

Copyright © 2003 Roger Hartley