Answer: Name mangling in C++, means, compiler gives different names to the overloaded functions to avoid ambiguity during functions call at compile time.

Recommended interview question: Explain function overloading in C++ with its return type and signature.

For example, in below class we have two overloaded functions with same name i.e. “func”. Now questions is, how does compiler understand these two functions as a different functions? Isn’t the functions call ambiguous for the compiler? Yes, it is ambiguous for compiler and that’s the reason compiler internally gives different names to these overloaded functions to avoid ambiguity.

This is called name mangling in c++.

class A
{
public:		
	void func(int a){		
	}	
	int func(int a, int b)
	{	
		return a+b;
	}	
};

NOTES:

  • C++ name mangling is also known as name decoration.
  • It is compiler dependent, hence, different compiler may mangle the same function in a different way.

This Question can also be asked as – How does compiler differentiate overloaded functions in C++?

Answer should be Name mangling plus above description.

FOCUS: In an interview please don’t stop answering this questions as just “Name Mangling”, but explain the above description too.

Related Posts