OOPortal OOPortal


Ad Hoc Polymorphism  «Prev  Next»
Lesson 9 Selection algorithm for function overloading
Objective Steps of overloaded function selection algorithm in correct order.

Selection Algorithm Function Overloading

Steps to overload function in C++

Whichever overloaded function is to be invoked, the invocation argument list must be matched to the declaration parameter list according to the function selection algorithm:
  1. Use an exact match if found.
  2. Try standard type promotions.
  3. Try standard type conversions.
  4. Try user-defined conversions.
  5. Use a match to ellipsis if found.

Standard promotions are better than other standard conversions. These are conversions from float to double, and from bool, char, short, or enum to int. Standard conversions also include pointer conversions.

Explicitly Casting Arguments

Standard Type Conversions

An exact match is clearly best.
Casts can be used to force such a match. The compiler will complain about ambiguous situations. Thus, it is poor practice to rely on subtle type distinctions and implicit conversions that obscure the overloaded function that is called. When in doubt, use explicit conversions to provide an exact match.
Signature matching has many subtleties and pitfalls. The current ANSI C++ standard document has over 50 pages of discussion and examples of the complexities of this topic.