C ++ Polymorphism

Source: Internet
Author: User

C ++ polymorphism and non-PolymorphismThe difference is whether the function address is bound early or late.. If the call address of the function can be determined during the compilation of the compiler, It is static, that is, the address is bound early. If the address of the function call cannot be determined during the compiler, that is, it must be determined at runtime, Which is late binding.

C ++ polymorphism:

1. Static Polymorphism

Using common functionsHeavy Load. Multiple Functions with the same name can be reloaded. The parameter lists of these functions are different, the number of parameters is different, or both are different. The compiler modifies the names of functions with the same name based on different lists of these functions to generate and process functions with different names to implement the overload of functions with the same name.

2. Dynamic Polymorphism

Dynamic polymorphism is through Virtual Functions The virtual function allows the subclass to redefine the member function, and the subclass to redefine the parent class is called overwrite ( Override ), Or rewrite. There are two types of rewriting: directly rewrite member functions and rewrite virtual functions.
The most common usage is to declare the pointer of the base class. This Pointer Points to any subclass object and calls the corresponding virtual function. Different methods can be implemented based on the different subclasses. If no virtual function is used When the base class pointer is used to call the corresponding function, it is always restricted to the base class function itself, and cannot call the function that has been overwritten in the subclass. . Because there is no polymorphism, the address of the function call will be certain, and the fixed address will always be called to the same function, which cannot implement an interface or multiple methods.
Polymorphism Function : Encapsulation can makeCodeModularization, inheritance can extend existing code, and their purpose is to reuse code. The purpose of polymorphism is to reuse interfaces. That is to say, no matter which class object is passed, the function can call the implementation method to adapt to the corresponding object through the same interface.
During compilation, the compiler finds that Base The class contains virtual functions. In this case, the compiler creates a virtual table for each class containing virtual functions (that is Vtable ). The table is a one-dimensional array that stores the addresses of each virtual function.
So how to locate the virtual table? The compiler also provides Virtual table pointer (that isVptr)This Pointer Points to the virtual table of the object. . In Program During runtime, Initialization is performed based on the object typeVptr So that Vptr Point to the virtual table of the class correctly, so that the correct function can be found when the virtual function is called.
It is precisely because the virtual functions called by each object are indexed by the virtual table pointer that determines the correct initialization of the virtual table pointer. In other words, we cannot call the virtual function before the virtual table pointer is correctly initialized. When or where is the virtual table pointer initialized? The answer is to create a virtual table and initialize the virtual table pointer in the constructor.
For a virtual function call, each object has a virtual table pointer, Which is initialized as a virtual table of this class. Therefore, no matter how your object type is converted in the program, the virtual table pointer inside the object is fixed. Therefore, dynamic object function calls can be realized, this is how C ++ polymorphism is implemented.

Conclusion (premise: the base class has virtual functions ):

1And each class has a virtual table.

2Virtual tables can be inherited. If the subclass does not override the virtual function, the virtual table of the subclass still has the address of this function, but this address points to the implementation of the virtual function of the base class. If the base class has threeVirtual functions, there are three items in the virtual table of the base class (virtual function address), and there are also virtual tables in the derived class. There are at least three items. If the corresponding virtual function is rewritten, then the address in the virtual table changes and points to its own virtual function implementation. If the derived class has its own virtual function, this item is added to the virtual table.

3The virtual function addresses in the virtual table of the derived class are arranged in the same order as those in the virtual table of the basic class.

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.