Override and overload (Overload)

zhaozj2021-02-16  42

Override and overload (overload) of C and Delphi

SpaceSoft [Dark Night Sand]

In object-oriented programming, when the subclasses have inherited the function from the base class, the subclasses may need to process some of these functions, such as:

Class chuman {public: void symyname () // Print an object's name {cout << "Hi, I am a human" << endl;}};

So very obvious, if his subclasses have a function of the same name, the same parameters and return values ​​(one sentence, one touch) function SaymyName, which function is it called? For example, there is now a Class Cmark

Class Cmark: Public Chuman {public: void symyname () {cout << "Hi, I am mar" << endl;}};

Then we have to ask, the following block:

Chuman * ph = new cmark; if (pH) pH-> SaymyName (); Else Cout << "Cast Error!" << ENDL;

DELETE PH; PH = NULL;

To print it out, is it really Hi, I am Mark?

Not. It outputs Hi, I am a human. This is very bad, when we point to a person who wants him to say his name, he told us that he "is a person" instead of saying his name. This problem occurs because the pointer to the base class is used to point to the public style class, which can access member functions inherited from the base class from the base class. However, if there is a function of the same name in the derived class, the result is still the same name function of the base class, not the function of derived class itself. In fact, we want to determine which one of the real types of an object will be determined which one of these same name functions, that is, such a resolution is dynamic. Or we can say that we hope that when an object is a subtype, its same name function is an implementation of the base class in the subclass.

Let's start from C to this problem.

This is a typical polymorphic example in C , and C uses virtual functions to achieve such a polymorphism. Specifically, use the Virtual keyword to explain the function to the virtual function, in the previous example, it should be declared:

Class chuman {public: Virtual void symyname () // Print an object's name {cout << "Hi, I am a human" << endl;}};

In this way, the other code is still the old, but our cmark already knows how to say his name. CMARK's SaymyName () function does not have a relationship with the description of the Virtual keyword, because according to the C syntax, because it covers the same name function of Chuman, it will become Virtual. As for why a virtual keyword has such a magical effect? The C FAQ Lite is described in this: In C , the "virtual member function is dynamically determined (at runtime). That is, the member function (at runtime) is dynamically selected, the selection is based on object-based type Instead of pointing to the type of pointer / references to this object. " So our PH found that he did actually pointing to a CMark type object, not a chuman declared by his own type, so it smartly called CMark's Saymyname. Delphi is the override of the function with the Override keyword. The covered function must be virtual (virtamic), that is, the function should include one of these two indication when declaring, such as:

PROCEDURE DRAW; Virtual;

When you need to be overwritten, you only need to re-declary with Override instructions in the subclass.

Procedure Draw; Override;

In the grammatical, the declaration is Virtual and Dynamic is equivalent. Their difference is that the former is optimized at speeds, while the latter optimizes the size of the code.

If the base class and subclasses contain the same function name and parameters, do not add Override instructions in the subclass? This is also correct in grammar. This means that the identity of the subclass has a hidden (HIDE) of the base class, although both the two exist in the derived class. Then returning to the first example of this article, when we point to a person if he wants him to say his name, he tells us that he "is a person", not to say his name.

It is worth noting that it is often not distinguished with us in C , which is not referred to as overload, in Delphi, only overload is the overload we usually say, The overloaded function still exists, relying on parameters to determine that the implementation is called. Of course, when the function of OverLoad and the function parameters of the base class, the implementation of the base class is dropped by HIDE, just like it mentioned above. Override is an unchaulous function that is covered, it is unaffected, and the original implementation is gone. Based on this reason, many articles are even incorrect, and the author thinks that it is not suitable.

Welcome to the author's personal homepage: http://www.mrspace.net/

转载请注明原文地址:https://www.9cbs.com/read-22769.html

New Post(0)