Reasons for C + + polymorphism

Source: Internet
Author: User

Polymorphism can be simply summed up as "1 interfaces, a variety of methods", in the process of the program to determine the call mechanism C + + compiler at compile time, to determine the address of the function called by each object, this is called early binding (early binding), the program implementation is so, By calling a function of a subclass from the parent class pointer, you can have multiple forms of the parent pointer. For the compiler to use late binding, the virtual keyword is used when declaring a function in a base class, which we call a virtual function. Once a function is declared as virtual in the base class, the function is virtual in all derived classes and does not need to be explicitly declared as virtual.

It is because the virtual function that each object calls is indexed by the virtual table pointer, it is very important to determine the correct initialization of the virtual table pointer. In other words, we are not able to invoke virtual functions until the virtual table pointers are properly initialized. So when, or where, is the virtual table pointer initialized?
The answer is to create the virtual table in the constructor and initialize the virtual table pointer. constructor invocation order, when constructing the subclass object, the constructor of the parent class is called, at which point the compiler only "sees" the parent class, does not know if there is a successor after it, initializes the virtual table pointer of the parent class object, and the virtual table pointer points to the virtual table of the parent class. When the constructor of a subclass is executed, the virtual table pointer of the subclass object is initialized, pointing to its own virtual table.

C + + polymorphism is summed up in a nutshell: By adding the virtual keyword to a function in the base class, overriding the function in a derived class, the runtime will invoke the corresponding function based on the actual type of the object. If the object type is a derived class, the function of the derived class is called, and if the object type is a base class, the function of the base class is called.



This article is from the "multiplication Formula" blog, please make sure to keep this source http://10706002.blog.51cto.com/10696002/1795156

Reasons for C + + 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.