Reload, overwrite, and hide in C ++ inheritance

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 take a look at someCodeAnd the compilation result.

Instance 1:
# Include "stdafx. H"
# Include
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 (INT argc, char * argv [])
{
Return 0;
}
Compiled
Error c2660: 'F': function does not take 1 Parameters

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, seeArticleThe last is a simple description, but I suggest you take a look at it step by step.
What we just said is that there is no virtual. What if there is virtual ??

Example 2:
# Include "stdafx. H"
# Include
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 (INT argc, char * argv [])
{
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!
Reload, overwrite, and hide in C ++ inheritance
Source: exam major [13:47:19] responsible editor: water streaming
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.
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.
Error c2555: 'CD: f': overriding virtual function differs from 'cb: f' only by return type or calling convention
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:
# Include "stdafx. H"
# Include
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 (INT argc, char * argv [])
{
Return 0;
}
Compilation error,
Error c2660: 'F': function does not take 1 Parameters
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.
What about other situations ?? As long as the name is the same and does not meet the conditions covered above, it is hidden.
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, 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.