Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++ how is function overloading typically implemented?

If there is no function overloading, the function name serves as the address of the function code, and when a function is being called, its address is easy to find using its name. However with function overloading, how exactly can the program find the correct function address? Is there a hidden table similar to virtual tables that stores the overloaded functions with their address? Thanks a lot!

like image 751
Rick Avatar asked Feb 09 '10 07:02

Rick


1 Answers

Name mangling.

It's all done at compile time. The C++ compiler actually modifies the function names you give it internally, so that a function like

int foo(int a, float b, char c) 

internally gets a name equivalent to

func_foo_int_float_char()

(the real symbol is usually some gobbledygook like ?CFoo@Foo@@QAAX_N@Z ).

As you can see, the name is decorated depending on the exact number and types of parameters passed. So, when you call a function, it's easy for the compiler to look at the parameters you are passing, decorate the function name with them, and come up with the correct symbol. For example,

int a, b; float f; char c;
foo(a,f,c) ; // compiler looks for an internal symbol called func_foo_int_float_char
foo(a,b,c) ; // compiler looks for a symbol called func_foo_int_int_char

Again, it's all done completely at compile time.

like image 113
Crashworks Avatar answered Oct 18 '22 11:10

Crashworks