[ fromfile: functions.xml id: overloading ]
In C++ the return type is not part of the signature.
C++ permits overloading of function names.
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.
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.
Example 5.1 shows a class with six member functions, each with a distinct signature.
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 {cout << m_Val << "\tdemo(int) const" << endl;} /* void demo(const int& n) {cout << ++m_Val << "\tdemo(int&)" << endl;} */ 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; };
Example 5.2. src/functions/function-call.cpp
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
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |