Concept distinction in C ++: Overload, Override, Hide

Source: Internet
Author: User

1.
Differentiation: overload, override, Hide


Overload: indicates multiple implementations with the same name but different number of parameters or different parameter types. (If the parameters are the same but the returned values are different, it is not an overload. The Compiler reports an error .)
The compiler determines the overload function:
The first step is to determine the set of overloaded functions considered in the call. This set is called the candidant function ). The so-called candidate function is a function with the same name as the called function.
The second step is divided into two actions: the first action is that the compiler calls up the viable function from the candidate function selected in the first step ). The number of feasible function parameters is the same as the number of called function parameters, or the number of feasible function parameters can be more, but the extra function parameters must have relevant default values; the second action is to convert the called function real parameters (conversion) into the real parameters of the candidate function based on the conversion rules of the parameter type. Here, we use the principle of making full use of parameter type conversion. In other words, we try to use parameter type conversion as much as possible. Of course, the conversion should be based on the candidate functions. If no feasible function is found according to the parameter conversion rules, the call is incorrect. no function matches the call and is not matched (no match function ).
Step 3: select the best match situation function from the viable functions selected in step 2 ). In the selection of the best feasible function, the conversion from the real parameter type of the function to the corresponding feasible function parameters must be classified. According to the classification (ranked), the best feasible function is selected.
Override: (unexpected translation) indicates that during inheritance, the parent class function is declared as vitual, And the subclass declares and implements the function again (the function name and parameter are identical, and the return value is not restricted ). Subclass: this function can be declared as virtual or not limited. However, if the subclass continues to be overloaded, it is best to declare it as virtual.
Hide: hidden functions in the inheritance structure. It indicates that a function is declared in the parent class. The sub-classes declare and define completely consistent functions, but the functions in the parent class are not declared as virtual, in this case, the function with the same name as the parameter in the subclass completely hides the function of the parent class. It seems like Override, but its implementation mechanism is completely different from that of Override, which may cause problems and should be avoided. (Virtual mechanisms are implemented using virtual tables. virtual tables maintain a list of virtual function pointers, but when a virtual function of an object is called, you can go to the virtual table to find and decide which function to call. For specific implementation, you can go to the unique blog .)
Class Super {public: void go () {cout <"go () called on Super" <endl ;}};
Class Sub: public Super {public: void go () {cout <"go () called on Sub" <endl ;}};

Sub mySub; mySub. go (); // output: go () called on Sub
Super & ref = mySub; ref. go (); // output: go () called on Super

Maybe you want it to call the subclass function, but this may be totally different from your expectation.

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.