C ++ overload override and hide

Source: Internet
Author: User

Feature of overload of member functions: (1) Same range (in the same class); (2) Same function name; (3) Different parameters; (4) virtual keywords are optional. Override refers to the function of a derived class that overrides a base class function. The features are as follows: (1) different ranges (located in the derived class and the base class respectively); (2) the function name is the same; (3) the parameters are the same. (4) basic functions must have virtual keywords. "Hide" means that the function of the derived class shields the base class function with the same name. The rule is as follows: (1) if the function of the derived class has the same name as the base class function, but the parameter is different. In this case, functions of the base class will be hidden regardless of whether there is any virtual keyword (Be sure not to confuse them with overload ). (2) If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual keyword. At this time, the base class functions are hidden (Be sure not to confuse with overwrite)

The key is that the following sentence helps me understand the specific reasons: these concepts are actually compiler problems.

Hidden (hide): indicates that the member functions of a derived class hide the member functions of a base class function. the word hidden can be understood as follows: when calling a class member function, the compiler will look up the function definition step by step along the inheritance chain of the class, if yes, the query is stopped. Therefore, if both a derived class and a base class have a function with the same name (whether or not the parameters are the same, the compiler finally selects the function in the derived class, so we can say that the member function of this derived class "hides" The member function of the base class, that is to say, it prevents the compiler from continuing to look up the function definition.

In polymorphism, the base class pointer can reference the override function in the derived class due to the virtual keyword modification. Under the link of the virtual table, use dynamic concatenation to find the corresponding function implementation in the required derived 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.