Brief Introduction to name mangling and Polymorphism

Source: Internet
Author: User

When reading the GCC source code, I saw the word Mangles, so I Googled it.

Before the emergence of object-oriented programming language, if you want to print different types of data, you need to write multiple methods, such as printinteger (int I), printstring (string S) and printfloat (float F ). That is to say, you must differentiate behavior and data types by naming them, because the former language, such as C, does not allow you to write with the same name, even if their parameter types are different.

C ++ comes to implement method overloading. Therefore, you can write multiple methods, such as printinteger (int I), printstring (string S), and printfloat (float f). The compiler will call specific print methods accurately. Method Overloading is supported by a technology called name mangling. In this technology, the compiler replaces the original method name by combining the original method name with its parameter to generate a unique internal name. In this way, when you call print (1), the compiler may internally rename the print method with a prefix from the parameter type, so that print (1) may become I _print (1 ).

The following is a more detailed example:

The C ++ compiler actually uses the following overload functions:

Voidprint (INTI );

Voidprint (charc );

Voidprint (floatf );

Voidprint (char * s );

Compiled:

_ Print_int

_ Print_char

_ Print_float

_ Pirnt_string

This function name uniquely identifies each function. Note: Different compiler implementations may be different, but they all use this mechanism. Therefore, when the connection calls print (3), it searches for functions such as _ print_int (3.

Next, let's talk about the problem. It is precisely because of this that overloading is considered not a polymorphism, while polymorphism is a dynamic binding at runtime ("multiple implementations of one interface"). If it is hard to think that overloading is a polymorphism, most of them are "polymorphism" during compilation ".

The compilation of variables in C ++ is similar. For example, global variables may be compiled into g_xx, and class variables may be compiled into c_xx. The connection is also based on this mechanism to find the corresponding variables.

Method Overloading is only a case of polymorphism. Name rename is a mechanism that supports method overloading. In more general cases, polymorphism is associated with inheritance. What is inheritance? Inheritance is a new class (called a subclass). It obtains partial definitions from the inherited class (called the parent class or superclass) and adds some new information. If you overload methods in the same class, the data types must be different. If you overload the method under the inheritance relationship, the method of the subclass and the parent class may be identical, and the name reconstructor generates the same rename.

For example, assume that a super class defines a print (int I) method and a subclass inherited from it also defines a print (int I) method. When you have a subclass instance, use polymorphism to call child. Print (INT). When you generate a parent class instance, use polymorphism to call parent. Print (INT ). This is the inheritance polymorphism: the same name and signature, but the class is different.

Inheritance polymorphism is achieved by using another mechanism related to name rename. The compiler places methods in a place called a virtual method table (VMT) (actually a method array. Each method has an index in VMT, so when print (INT) is called, the compiler will be routed to VMT to find the internal index of the print method and class. In this way, the compiler can call the correct method for implementation. The compiler is responsible for managing all VMT indexes and class offsets.

In short, polymorphism allows you to define many methods with very similar names. The names here are often intuitive and easy to remember. The OOP compiler will understand which method to call based on the caller class.

For more information about name mangling, see the http://en.wikipedia.org

Brief Introduction to name mangling and Polymorphism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.