Clause 21: Reload is not the same as rewrite. Clause 21: Reload rewrite

Source: Internet
Author: User

Clause 21: Reload is not the same as rewrite. Clause 21: Reload rewrite

There is no relationship between Overloading and rewriting, but in the process of use, due to some function naming issues do not pay attention to, we have produced countless bugs.

When two or more functions in the same scope have the same name but different number of parameters, or functions with the same number but different parameter types are overloaded. That is, when a function is called, The most matched function is selected based on the parameter list; -- in this way, the same function name (Operation Method) is formed ), different parameters (different raw materials) are used to call different functions (different results ).

Rewrite: When a derived-type function and a base-class virtual function contain the same name and parameter list, when a base-class function is called, the base-class virtual function is called, when a subclass is called, it calls the virtual function of the subclass and overwrites it. (the call here refers to a pointer or referenced call, because only in this way can dynamic behavior binding be performed) -- This is a rewrite. The same function name (Operation Method) is executed by different classes in the class hierarchy (different operators are executed ), this leads to different function calls (different results) -- this is the rewrite. -- This implementation is: when the interface of the base class Convention remains unchanged, the virtual function is implemented through the subclass to change the specific implementation of the specific class.

Example:

   1:  class Base
   2:  {
   3:  public:
   4:       //...
   5:      virtual int f ( int );
   6:      void f( Base *);
   7:  };
   8:   
   9:  class D:: public Base
  10:  {
  11:  public:
  12:      //...
  13:      int f(int );
  14:      int f( B *);
  15:  };

In the above example, we can see that

D: f (int) and Base: f (int) of the Base class form rewrite will be dynamically bound. In this case, a heavy load relationship is formed between D: f (int) and D: f (B.

Base: f (int) and Base: f (Base *) form an overloaded relationship,

Note that there is a tangle between the class designers, that is, D: f (int) and Base: f (int) of the Base class. They rewrite each other, the code can call and bind dynamically. The calling in the class hierarchy has different effects, but the function at the same level is overloaded separately. I have to say this is a very bad design.

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.