Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Current »

How is it that we can overload a function to accept different input parameters in a language like C++? In C we are not allowed to do this. How does the compiler represent different entities with the same identifier?

video12409853581.mp4

In the compiler construction, languages like C++ encode information into symbol names to contain information such as parameter types and return types.

This enables support for features such as overloading and bug detection - to check for things like the wrong assignment of types or wrong inputs etc.

Simple Example

Two C++ functions defined named f():

int  f () { return 1; }
int  f (int)  { return 0; }
void g () { int i = f(), j = f(0); }

These are distinct functions, with no relation to each other apart from the name. The C++ compiler will therefore encode the type information in the symbol name, the result being something resembling:

int  __f_v () { return 1; }
int  __f_i (int)  { return 0; } 
void __g_v () { int i = __f_v(), j = __f_i(0); }

Even though its name is unique, g() is still mangled: name mangling applies to all C++ symbols (those not in an extern "C"{} block).

To avoid name mangling in C++, define symbols in an extern "C"{} block. This interprets the block as C code rather than C++.

  • No labels