Search code examples
c++runtimeswitch-statementmemberoverloading

How does function overloading work at run-time, and why overload?


Let's say I have a class named ClothingStore. That class has 3 member functions, that point a visitor to the right department of the store. Member functions are ChildrenDept, MenDept and WomenDept, depending on whether the visitor is a child, a man or a woman.

Function overloading can be used to make 3 functions that have same name, say, PointToDept, but take different input argument ( child, man, woman ).

What is actually happening on run-time when program is executing ?

My guess is that compiler adds switch statements to the program, to select the right member function. But that makes me wonder - is there any benefit in terms of program performance when using overloaded functions, instead of making your own function with switch statements? Again, my only conclusion on that part is code readability. Thank you.


Solution

  • From Gene's comment:

    The compiler sees three different functions just as though they had been differently named.

    In the case of most compilers, they are differently named. This used to be called name mangling where the function name is prefixed by return type and suffixed by the parameter types.