[C ++ topic] overload, override, and hide

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, the function name is F (we don't care what the parameter is ), if an F member function is declared in the CD of the derived class (regardless of whether the parameters are the same), all the f members in the base class will be hidden in the class CD domain.

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 functions of a derived class overwrite the functions 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. What is the same? function name, parameter, and return type?[1]Three conditions.Parameter types cannot be overwritten even if there is an inheritance relationship.

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
[1] There is an exception,If the returned value of the parent class is a pointer or reference, the Child class override can return the derivation of this pointer (or reference ).

In addition to the function name/parameter must be identical, add the const attribute to the member function. The base class has the const attribute, and the derived class must also contain the const attribute.

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

The following are some declaration statements to check which are override and hide.

 

1 ).
Base * Base: Copy (Base * );
Base * Derived: Copy (derived * );
// Hide
2 ).
Base * Base: Copy (Base * );
Derived * Derived: Copy (Base * );
// Override
3 ).
Base base: Copy (Base * );
Derived derived: Copy (Base * );
// Error
3 ).
Ostream & Base: Print ( Int , Ostream & = Cout );
Ostream & Derived: Print ( Int , Ostream & );

4).
VoidBase: eval ()Const;
VoidDerived: eval ();

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 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 overload:

Overload ):Must be in a domainFunction names are the same, but function parameters are different. The function of overloading has different behaviors. Therefore, functions in a domain cannot constitute overloading. This is an important feature of overloading.

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.

So,For a function with the same name, the relationship 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.