5.1.  Overloading Functions

[ fromfile: functions.xml id: overloading ]

As stated in Section 1.5 the signature of a function consists of its name and its parameter list. In C++ the return type is not part of the signature.

You have seen that C++ permits overloading of function names. Recall that 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. Also recall (Section 1.5) that 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. For the purpose of signature matching, Type and Type& parameters match.

This is the sequence of steps that the compiler takes to determine which overloaded function to call.

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

  2. Else, match through standard type promotions (Section 19.6).

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

  4. Else, match through ellipsis (...) (Section 5.11) (if found).

  5. Else, report an error.

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

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

[ . . . . ]

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

1

Overloaded on const-ness.

2

Clashes with previous function.

<include src="src/functions/function-call.cpp" mode="cpp" href="src/functions/function-call.cpp" id="signaturedemo" segid="classdef"/>


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);
    sd.demo(2);    
    csd.demo(2);   1
    int i = 3;
    sd.demo(i);
    short s = 5;
    sd.demo(s);
    csd.demo(s);   2
    sd.demo(2.3);  3
    float f(4.5);   
    sd.demo(f);
    csd.demo(f);    
    // csd.demo(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.

<include src="src/functions/function-call.cpp" mode="cpp" href="src/functions/function-call.cpp" id="functioncallcpp" segid="clientcode"/>


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