140: Name Mangling and Overloaded Methods.

Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categories:

You normally don’t have to worry about name mangling. But you should know what it is. This sounds a lot worse than it is. Name mangling is a technique that compiler vendors use to give overloaded methods unique names. It’s also sometimes called name decoration. You’ll likely see strange names that don’t match what you might expect when debugging or when trying to understand linker errors. Before C++ or other languages that allow you to create methods with the same name, each method had to have a unique name. It’s easy to take for granted this ability to name methods with the same name. At some point though, the compiler needs to know exactly which method will be called. And the linker also needs to be able to identify one method from another. We can’t use a simple method name for this when that’s not unique. What most languages do is enforce that the method name along with other factors such as the parameter types is unique. At least that’s what we see when programming. But behind the scenes, the compiler renames methods by using combinations of the method name, special characters, the parameter types, and possibly the calling convention, return type, class name, and other factors. It’s not a very readable name. And there’s no standard way that compilers follow. They’re all different even sometimes between versions. Sometimes, the name mangling will be standardized. This is usually for simple languages like C so that different compiler vendors can make operating system calls. Yes, even the C language that doesn’t allow method overloading can benefit from name mangling to be able to distinguish how a method should be called. this is called the method calling convention. Listen to the episode, or read the full transcript below, for more details including why compiler vendors don’t just agree on a standard naming system. Transcript This sounds a lot worse than it is. Name mangling is a technique that compiler vendors use to give overloaded methods unique names. It’s also sometimes called name decoration. You’ll likely see strange names that don’t match what you might expect when debugging or when trying to understand linker errors. Before C++ or other languages that allow you to create methods with the same name, each method had to have a unique name. Let’s say that you’re writing an adventure game and want to allow the hero to be healed. There are different ways to heal the hero and two of them might be through the use of bandages or magic. Both methods heal the hero but need different parameters. You couldn’t have two methods both called heal so instead you needed to create a healByBandage method that takes a bandage parameter and another healByMagic method that takes a spell. This works but you have to come up with unique names for each method. Sometimes the names aren’t as easy as this. Maybe several parameters are involved. Do you really want another method called healByBandageAndOintment? And then there’s constructors that you can’t just rename however you want. Depending on the language, a constructor usually has to match the class name. Creating a class that’s flexible and can be created with a default constructor, a copy constructor, and several other overloaded constructors would not be possible without method overloading. It’s easy to take for granted this ability to name methods with the same name. At some point though, the compiler needs to know exactly which method will be called. And the linker also needs to be able to identify one method from another. We can’t use a simple method name for this when that’s not unique. What most languages do is enforce that the method name along with other factors such as the parameter types is unique. This means that because the healByBandage method takes a bandage parameter and the healBySpell method takes a spell parameter, t