22.14 Name-mangling

22.14 Name -mangling

When a compiler processes C++ code it changes the names of functions to include information about the types of the function's arguments. This process, normally invisible, is called 'name-mangling'.

You most often notice the effects of name-mangling when you put a prototype of a class method in a header file, but then fail to implement the method in any of your project's *.cpp files. The project will compile all right, but at the very end, you will get a linker message. Thus, if you were to leave out the code for the cCritter method int cCritter::move(Real dt) , you would get a linker error like the following.

 popview.obj : error LNK2001: unresolved external symbol "public: int      __thiscall cCritter::move(float)" (?move@cCritter@@QAEHM@Z) 

The string at the end at the end of the line is the name-mangled name of the method.

Remember how we talked about how having a const after the name of a function changes how C++ thinks of it? That's because the const goes into the name-mangled name.

Name-mangling can become an issue in advanced programming situations, for instance if (a) you want to link someone else's *.c module with your *.cpp modules, or (b) you want to locate a function by name inside a dynamic link library module. This is because although you may think the function's name is what you called it, say ' int MyFunction() ,' C++ will actually have assigned a different 'mangled' name for your function. You can turn off the name-mangling for a function name by specifically asking in its prototype that it be treated like a C function, using a line like the following.

 extern "C" int MyFunction(); 

Software Engineering and Computer Games
Software Engineering and Computer Games
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net