Overload, override, and hide in C ++)

Source: Internet
Author: User
Before writing a question, give a few keywords in Chinese and English, overload, override, and hide ). In early C ++ books, people who translate may not be familiar with professional terms (nor can they blame them. They are not engaged in computer programming and they are specialized in English ), overload and override are often incorrect!

Let's first look at some code and its compilation results.

Instance 1:

Code: select all
#include <iostream>
using namespace std;

class CB
{
public:
    void f(int)
    {
        cout << "CB::f(int)" << endl;
    }
};

class CD : public CB
{
public:
    void f(int, int)
    {
        cout << "CD::f(int, int)" << endl;
    }

    void test()
    {
        f(1);
    }
};

int main()
{
    return 0;
}

Compiled
[Eric @ rockmane override_overload_hide] $ g ++-O test1 test1.cc
Test1.cc: In member function 'void CD: Test ()':
Test1.cc: 23: Error: no matching function for call to 'CD: F (INT )'
Test1.cc: 17: Note: candidates are: void CD: F (INT, INT)

Conclusion: In the CD-like domain, there is no function such as F (INT), and void F (INT) in the base class is hidden.
If you change the declaration of the member function void F (INT, INT) in the derived CD to the same as that in the base class, that is, F (INT), void F (INT) in the base class) if it is still overwritten, compilation will not go wrong. In the function, test calls F (INT) in CD)
Therefore, if some functions in the base class do not have the virtral keyword and the function name is F (whatever the parameter is), if an F member function is declared in the derived class CD, in the class CD domain, all the F in the base class are hidden.

If you are in a hurry and want to know what is hidden, read the simple description at the end of the article, but I suggest you continue to read it step by step.
What we just said is that there is no virtual. What if there is virtual ??

Example 2:

Code: select all
#include <iostream>
using namespace std;

class CB
{
public:
    virtual void f(int)
    {
        cout << "CB::f(int)" << endl;
    }
};

class CD : public CB
{
public:
    void f(int)
    {
        cout << "CD::f(int)" << endl;
    }
};

int main()
{
    CD cd_obj;
    cd_obj.f(1);
    CB cb_obj;
    cb_obj.f(1);

    CB *pCB_Obj = new CD();
    pCB_Obj->f(1);
    delete pCB_Obj;
    return 0;
}

Of course there is no problem with writing this. Here I don't have to pay much for it. This is very simple, with polymorphism and virtual functions. Then what is the pointer to the base class to point to the derived class object, by referencing and calling a virtual function, there are many attributes. What ?? You don't understand ?? Find a C ++ book and explain the polymorphism and virtual function mechanism !!
In this case, we call override )! Overwrite indicates that the virtual function of the derived class overwrites the function of the base class with the same name and same parameters!

Here, I want to emphasize that such coverage must meet two conditions.
(A) With the virtual keyword, you can add it to the function declaration in the base class.
(B) The functions in the base class CB should be exactly the same as those in the derived class CD. The functions are called exactly the same. The function names, parameters, and return types are three conditions.

Note: If the function definitions of the derived class and the function definitions of the basic class are different, if the function parameter list is different, the compilation will not fail, but cannot be overwritten, the function of the base class is hidden. If the return value of the function of the derived class is different from that of the base class, a compilation error occurs. The error message indicates that the return types of the two functions are different, but here is a special case: if the virtual function of the base class returns a pointer or reference to itself, such as baseclass *, when the derived class overwrites this virtual function, the return value of the function can be written as the derived class itself, for example, derivedclass *, which can constitute a coverage, and the polymorphism is true. This is the only special case. For details, refer to the description in C ++ primer and the experiment has been tested, yes.

Some people may question the statement in (B), saying that the return type should be the same ??
Yes, it must be the same if it is overwritten. I tried it. If the declaration of F is changed to virtual int F (INT) in the base class, the compilation fails.

Test2.cc: 17: Error: conflicting return type specified for 'virtual void CD: F (INT )'
Test2.cc: 8: Error: overriding 'virtual int CB: F (INT )'

Therefore, the preceding (a) (B) conditions must be met.

If the f keyword of the function in the base class CB is virtual, but the parameter is different from the F parameter of the function in the derived class CD,
Example 3:

Code: select all
#include <iostream>
using namespace std;

class CB
{
public:
    virtual void f(int)
    {
        cout << "CB::f(int)" << endl;
    }
};

class CD : public CB
{
public:
    void f(int, int)
    {
        cout << "CD::f(int, int)" << endl;
    }

    void test()
    {
        f(1);
    }
};

int main()
{
    return 0;
}

Compilation error,
[Eric @ rockmane override_overload_hide] $ g ++-O test3 test3.cc
Test3.cc: In member function 'void CD: Test ()':
Test3.cc: 23: Error: no matching function for call to 'CD: F (INT )'
Test3.cc: 17: Note: candidates are: void CD: F (INT, INT)

Why ?? What is a common mistake ?? Yes, it is the same as in instance 1. The conclusion is that the function in the base class is hidden.

Through the above three examples, we can draw a simple conclusion.
If the function f with the same name as the function F in the base class and the derived class meets the following two conditions:
(A) There are virtual keywords in function declaration in the base class.
(B) The functions in the base class CB are exactly the same as those in the derived class CD. function names, parameters, and return types are all the same.
So this is called override, which is also a virtual function, the nature of polymorphism. But there are special cases. Let's take a closer look at the red letter above.

What about other situations ?? As long as the name is the same and does not meet the conditions covered above, it is hidden.

In addition, I also tested the last case, that is, CB defines virtual F, and then CD does not define F function, then, call F (1) in the test method of CD to compile and run normally. This proves that F is inherited normally.

Next I want to talk about the most important part. Many people think that F (INT) in the base class CB will inherit and F (INT, INT) in the CD will constitute an overload in the derived class CD, as you can imagine in instance 1. Right? Let's first look at the definition of heavy load.

Overload ):
A function must have the same name but different function parameters in a domain. The function of overloading has different behaviors for the same function. Therefore, a function in a domain cannot constitute an overload, this is an important feature of heavy load.
It must be in a domain, but inheritance is obviously in two classes, so the above idea is not true. The same is true for the structure we tested. F (INT, INT) in the derived class) hides F (INT) from the base class.
Therefore, for functions with the same function name, the relationships between the base class and the derived class can only be overwritten or hidden.

In the article, I gave both the definition of overload and coverage, but I never gave them a hidden definition. At the end, I gave it, this is a long term from Google on the Internet. You can simply understand that in the derived class domain, you cannot see the function with the same name in the base class, or, is not inherited for you to use, huh, like instance 1.

Hide (hide ):
It indicates that the member function of the derived class hides the member function of the 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.

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.