InterviewSolution
| 1. |
What is Name Mangling in C++? Write an example for the same? |
|
Answer» Name mangling is the process through which the C++ compilers give each FUNCTION a in program a unique name. In C++, all programs have at-least a few functions with the same name. Name mangling is a concession to the FACT that LINKER always INSISTS on all function names being unique. In C++, generally programs have at-least a few functions with the same name. Example: In general, member names are made unique by concatenating the name of the member with that of the class given the declaration: class Bar { public: int ival; ... };ival becomes something like: // a possible member name mangling ival__3BarConsider this derivation: class Foo : public Bar { public: int ival; ... }The internal representation of a Foo object is the concatenation of its base and derived class members. // Pseudo C++ code // Internal representation of Foo class Foo { public: int ival__3Bar; int ival__3Foo; ... };Unambiguous ACCESS of either ival members is achieved through name mangling. Member functions, because they can be overloaded, require an extensive mangling to provide each with a unique name. Here the compiler generates the same name for the two overloaded instances(Their argument lists make their instances unique). |
|