A/C + + static polymorphism and dynamic polymorphism

Source: Internet
Author: User

Static polymorphism is the process of determining which functions, such as overloading of functions, will be executed here during system compilation.

Dynamic polymorphism is the use of virtual functions to achieve the runtime polymorphism, that is, when the system compiles do not know which function the program is going to call, only when running here to determine which function to jump next to the stack frame.

The virtual function is the declaration in the base class that the function is virtual (before the function is added to the dummy keyword), and then formally defined in the subclass (the function name, return value, function parameter number, parameter type of the class in the subclass, all the same as the virtual function declared by the base class. Otherwise, it is overloaded with the function, then define a pointer to the base class object, then point the pointer to the subclass object derived from the base class, and then use the pointer to invoke the virtual function to implement the dynamic polymorphism.

Function name in the same case:

Overloads (scopes are the same, the number of parameters or the order or type are different, whether or not virtual functions). are essentially different functions.

Overrides, or overrides, differ in scope, with the same number of arguments, which must be a virtual function. The essence is to replace the function address of the virtual function table at the time of inheritance.

Hidden (scopes differ, regardless of whether the parameters are the same, whether or not virtual functions). Occurs when a child scope is entered from the parent scope.

conclusion, the same scope must be overloaded otherwise redefined, the scope is different if the parameter is the same and the virtual function is overwritten otherwise is hidden.

For overloads:

Const type and type do not constitute overloads

Const type& and type& form overloads

Const type* and type* form overloads

A/C + + static polymorphism and dynamic 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.