C++ function overloading

Function overloading is a very straightforward concept in C++; multiple different functions can have the same name. That's it, that is all there is to overloadingwhen the compiler sees syntax that indicates a function call, formatted as f(x), then there must be more than one function named f. If this happens, we are in an overload situation, and overload resolution must take place to find out which of these functions should be called.

Let's start with a simple example:

void f(int i) { std::cout << "f(int)" << std::endl; }        // 1void f(long i) { std::cout << "f(long)" << std::endl; }      // 2void f(double i) { std::cout << "f(double)" << std::endl; }  // 3f(5);f(5l);f(5.0);

Here, we have three function definitions for the ...

Get Hands-On Design Patterns with C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.