Function relationships with the same name in the class, overload, overwrite/overwrite, hide, function overload

Source: Internet
Author: User

Function relationships with the same name in the class, overload, overwrite/overwrite, hide, function overload

For functions with the same name in the class, overload, overwrite/overwrite, hide

C ++ features, not a class, can also be overloaded, and is not associated with virtual.
Overload: the same class or non-class domain, the same function name, the number or type of different form parameters; the form parameter determines which function to call. A compilation error occurs when the same parameters of functions with the same name in the same domain are the same.

 

The virtual modifier requires the same return value and parameter type. This modifier is required for polymorphism.

Rewrite/overwrite: the relationship between the base class and the derived class. The same function name, number and type of the same form parameter, and the base class function has virtual modification. The derived class executes the function of the derived class instead of the base class function, the polymorphism between the pointer and the reference is embodied, that is, the base class pointer or reference can execute the corresponding derived class implementation based on the Type pointing to the actual subclass. If you want to execute the base class function, you can still specify it through the class domain.

 

/*************************************** ***********************/

Hide: the relationship between the base class and the derived class, the same function name, the same number and type of parameters, the base class function does not have virtual modification; the class field is not specified, and the variable type determines which function is used for implementation.

Even if the base class Pointer Points to a derived class, calling a function with the same name still calls the base class function, because the non-virtual modifier cannot reflect polymorphism.

 

Note:
The relationship between the base class and the derived class, the same function name, the number and type of different parameters, the base class function has/does not have virtual modifier; the compiler determines the implementation of the class to be called based on the real parameter, if the current class variable type is not declared, the compilation error occurs.
1. the compiler will find the current derived class declaration for the derived class variables (including pointers and references). The declaration of the base class is invalid and the subclass cannot be inherited.
2. the compiler will find the base class declaration for the base class variables (including pointers and references). The subclass declaration is invalid and the polymorphism cannot be reflected. Even if the base class Pointer Points to the subclass, the compilation still fails if the base class is not declared.

 

Therefore, hiding is just a saying, not a rule. Understand the C ++ compiler and know that the compiler uses parameter identification, and then finds the declaration in the class. If yes, it calls and if no, an error is returned.

Edit by yexf

 

Related Article

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.