How does the overload function work at run time and why does overload?

Say I have a class called ClothingStore. This class has 3 member functions that point the visitor to the correct department store. The functions of the members are ChildrenDept, MenDept and WomenDept, depending on whether the visitor is a child, a man or a woman.

Functional overload can be used to create three functions that have the same name, for example, PointToDept, but accept different input arguments (child, man, woman).

What actually happens during program execution?

I assume that the compiler adds switch statements to the program to select the correct member function. But this makes me wonder if there is any benefit in terms of program performance when using overloaded functions instead of doing your own function with switch statements? Again, my only conclusion in this part is code readability. Thanks.

+4
source share
3 answers

From gene comment:

The compiler sees three different functions, as if they were named differently.

- . name mangling, .

+2

, switch , -.

. ++ - . . , , - , . 13.3 , , , . , . - . ++.

+6

. . .

and why overloading? ... is there any benefit in terms of program performance when using overloaded functions instead of doing its own function with switch statements?

Yes. There is no overhead at runtime compared to "creating your own function using switch statements".

+4
source

Source: https://habr.com/ru/post/1525715/


All Articles