next up previous
Next: typename prefix in C++ Up: C++/C Previous: Automatic indenting


C++ name mangling

In C++, we can define functions with same name but different argument types or in different scopes (Overloading); So the symbol in the C++ module need to be mangled so that we can uniquely identify a function within different scope or differnt argument types. For example for the below C code
int foo(double*);
double bar(int, double*);

int foo (double* d) 
{
    return 1;
}

double bar (int i, double* d) 
{
    return 0.9;
}

Its symbol table would be (by ``dump -t'')

[4]	 0x18        44       2		1	0	0x2	bar
[5]	 0x0         24       2		1	0	0x2	foo

For same file, if compile in g++, then the symbol table would be

[4]	 0x0         24       2		1	0	0x2	_Z3fooPd
[5]	 0x18        44       2		1	0	0x2	_Z3bariPd
``_Z3bariPd'' means a function whose name is bar and whose first arg is integer and second argument is pointer to double.

Now if we enbrace the above code with

extern "C"  {
...
}
This mean the code will be C style linkage; Then the symbol table is same as the C code
[4]      0x18        44       2         1       0       0x2     bar
[5]      0x0         24       2         1       0       0x2     foo

If we put the functions into Class definition

class Foo 
{
public: 
    int foo (double*);
    double bar (int, double* );
};

int Foo::foo(double* d) 
{
    return 1;
}

double Foo::bar(int i, double*)
{
    return 0.9;
}
Then the mangled name will include the Class inforation, (symbol will be)
 
[4]	 0x1c        48       2		1	0	0x2	_ZN3Foo3barEiPd
[5]	 0x0         28       2		1	0	0x2	_ZN3Foo3fooEPd



Wei Lu 2007-11-06