Overloading Functions

The signature of a function consists of its name and its parameter list. In C++, the return type is not part of the signature.

C++ permits overloading of function names. A function name is overloaded if it has more than one meaning within a given scope. Overloading occurs when two or more functions within a given scope have the same name but different signatures. It is an error to have two functions in the same scope with the same signature but different return types.

Function Call Resolution

When a function call is made to an overloaded function within a given scope, the C++ compiler determines from the arguments which version of the function to invoke. To do this, a match must be found between the number and type of the arguments and the signature of exactly one of the overloaded functions. This is the sequence of steps that the compiler takes to determine which overloaded function to call.


If there is an exact match with one function, call it.


Else, match through standard type promotions (see Section 19.5).


Else, match through conversion constructors or conversion operators (see Section 2.12).


Else, match through ellipsis (...) (see Section 24.1), if found


Else, report an error.

Example 5.1 shows a class with six member functions, each with a distinct signature. Keep in mind that each function has an additional implicit parameter: this. The keyword const, following the parameter list, protects the host object from the action of the function and is part of its signature.

Example 5.1. src/functions/function-call.cpp

[ . . . . ]

class SignatureDemo {
 SignatureDemo(int val) : m_Val(val) {}
 void demo(int n)
 {cout << ++m_Val << "	demo(int)" << endl;}
 void demo(int n) const 	 <-- 1
 {cout << m_Val << "	demo(int) const" << endl;}
/* void demo(const int& n)
 {cout << ++m_Val << "	demo(int&)" << endl;} */ <-- 2	
 void demo(short s)
 {cout << ++m_Val << "	demo(short)" << endl;}
 void demo(float f)
 {cout << ++m_Val << "	demo(float)" << endl;}
 void demo(float f) const
 {cout << m_Val << "	demo(float) const" << endl;}
 void demo(double d)
 {cout << ++m_Val << "	demo(double)" << endl;}
 int m_Val;

(1)overloaded on const-ness

(2)clashes with previous function

Example 5.2 contains some client code that tests the overloaded functions from SignatureDemo.

Example 5.2. src/functions/function-call.cpp

[ . . . . ]

int main() {
 SignatureDemo sd(5);
 const SignatureDemo csd(17);
 csd.demo(2); <-- 1
 int i = 3;
 short s = 5;
 csd.demo(s); <-- 2
 sd.demo(2.3); <-- 3
 float f(4.5);
 return 0;

(1)const version is called.

(2)Non-const short cannot be called, so a promotion to int is required to call the const int version.

(3)This is double, not float.

The output should look something like this:

6 demo(int)
17 demo(int) const
7 demo(int)
8 demo(short)
17 demo(int) const
9 demo(double)
10 demo(float)
17 demo(float) const


Exercises: Overloading Functions


Experiment with Example 5.1. Start by uncommenting the third member function and compiling.


Try adding the following line just before the end of main():


What happened? Explain the error message.


Add other function calls and other variations on the demo() function.

Explain each result.

Part I: Introduction to C++ and Qt 4

C++ Introduction


Introduction to Qt



Inheritance and Polymorphism

Part II: Higher-Level Programming


Introduction to Design Patterns


Generics and Containers

Qt GUI Widgets


Validation and Regular Expressions

Parsing XML

Meta Objects, Properties, and Reflective Programming

More Design Patterns

Models and Views

Qt SQL Classes

Part III: C++ Language Reference

Types and Expressions

Scope and Storage Class

Statements and Control Structures

Memory Access

Chapter Summary

Inheritance in Detail

Miscellaneous Topics

Part IV: Programming Assignments

MP3 Jukebox Assignments

Part V: Appendices

MP3 Jukebox Assignments


MP3 Jukebox Assignments

show all menu

An Introduction to Design Patterns in C++ with Qt 4
An Introduction to Design Patterns in C++ with Qt 4
ISBN: 0131879057
EAN: 2147483647
Year: 2004
Pages: 268
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net