C + + and Delphi function overlay (Override) and overload (overload

Source: Internet
Author: User
Tags functions
c++| functions C + + and Delphi function overlay (Override) and overload (overload)

Spacesoft "Night Sand"

In object-oriented programming, when a subclass inherits a function from a base class, a subclass might need to treat some of these functions differently from the base class, such as:

Class Chuman
{
Public
void Saymyname ()//print out the name of the object
{
cout << "Hi, I am a human" << Endl;
}
};

So obviously, if his subclass had a function saymyname with the same name, the same argument and the return value (a word, one touch), which function would it call? For example, now there is a class Cmark

Class Cmark:public Chuman
{
Public
void Saymyname ()
{
cout << "Hi, I am Mark" << Endl;
}
};

So we're going to ask, the following section of the program:

Chuman *ph = new Cmark;

if (PH)
Ph->saymyname ();
Else
cout << "Cast error!" << Endl;

Delete PH;
PH = NULL;

To print out, really we want hi, I am mark?

No. It outputs Hi, I am a human. It's bad, when we point to someone asking him to say his name, he tells us he's "a person", not his name. This problem occurs because the pointer to the base class points to the public derived class, and you can access the member functions that the derived class inherits from the base class. However, if a function of the same name is also in the derived class, the result is still accessing the function of the same name as the base class, not the function of the derived class itself. In fact, what we want is a real type of object that determines which of these functions are invoked, that is, the resolution is dynamic. Or we can say, we want when an object is a subtype, its function of the same name in subclasses overrides (override) the implementation of the base class.

Let's start with C + + on this issue.

This is a typical polymorphic example in C + +, and C + + uses virtual functions to achieve this polymorphism. Specifically, the virtual keyword is used to describe the function as a virtual function, in the previous example should be declared:

Class Chuman
{
Public
virtual void Saymyname ()//print out the name of the object
{
cout << "Hi, I am a human" << Endl;
}
};

In this way, the other code is the same old, but our cmark already know how to say their name. Cmark's Saymyname () function is not related to the description of the virtual keyword, because according to C + + syntax, because it covers the Chuman function of the same name, it becomes virtual. As for why a virtual keyword has so magical effect? C + + FAQ Lite explains this: in C + +, "virtual member functions are dynamically determined (at run time)." That is, a member function (at run time) is dynamically selected, which is based on the object's type rather than the pointer/reference type of the object. So our ph finds itself in fact pointing to a Cmark type of object, not the Chuman of its own type, so it intelligently invokes the Cmark saymyname.


And Delphi is using the Override keyword to explain function coverage. The overridden function must be virtual (virtual) or dynamic, that is, the function should contain one of these two indicators when declared, such as:

Procedure Draw; Virtual

When you need to overwrite it, just use the override indicator in the subclass to make a declaration again.

Procedure Draw; Override

In syntax, declaring virtual and dynamic is equivalent. The difference is that the former optimizes speed in implementation, and the latter optimizes the size of the code.

What if the base class and subclasses all contain the same function name and parameter, and do not add override to the subclass? This is grammatically correct, too. This means that the implementation of a subclass's function with the same name hides the implementation of the base class, although both are present in the derived class. So it goes back to the first example of this article: when we point to a person asking him to say his or her name, he tells us that he is "a person", not his name.

It is noteworthy that with our in C + + often do not distinguish between a function and overload of a function known as overloading different, in Delphi, only overload (overload) is what we usually say overload, the overloaded function is still there, depending on the parameters to determine the end of the call that implementation. Of course, the implementation of the base class is removed, as mentioned above, when the overload function and the function parameters of the base class are identical. and the overlay (override) is to let the covered function is not visible, indeed the "cover" off, the original implementation is gone. For this reason, many articles and even some books are wrong to translate override into overloading, I think it is not appropriate.



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.