# Include <iostream>
Using namespace STD;
Struct foo
{
Void func (int x ){
Cout <"foo: func (INT)" <Endl;
}
Void func (float X ){
Cout <"foo: func (float)" <Endl;
}
Void func (Double X ){
Cout <"foo: func (double)" <Endl;
}
};
Struct
{
Void func (int x ){
Cout <"struct a: Fun (int x)" <Endl;
}
};
Struct B: public
{
Void func (float X ){
Cout <"struct B: Fun (float X)" <Endl;
}
};
Struct C: Public B
{
// Using a: func;
// Using B: func;
Void func (Double X ){
Cout <"struct C: func (Double X)" <Endl;
}
};
Int main ()
{
Foo fo;
Fo. func (1 );
Fo. func (float) 1 );
C OC;
OC. func (1 );
OC. func (float) 1 );
}
The running result is:
Foo: func (INT)
Foo: func (float)
Struct C: func (Double X)
Struct C: func (Double X)
It indicates that the previous part is an overload, and the subsequent part is completely overwritten. c ++ has a rule that the derived class and the base class are in two different namespaces. Therefore, the code after splitting should not be considered heavy load. If you want to reload data, you must use the following using statement:
Struct C: public
{
Using a: func;
Using B: func;
Void func (Double X ){}
};
So, if you do not need the using declaration, what should be the function func in the code after the split not to be reloaded? Is hidden (hide ). The differences between overload, overwrite, and hide are as follows:
These concepts share one thing in common: function names are the same, so they are confusing. The general differences are as follows:
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.
Override ):
Override refers to the virtual function of a derived class that covers the functions with the same name and same parameters of the base class. Since it is linked to a virtual function, it indicates that this is a feature supported by polymorphism, the so-called overwrite refers to a function that is called based on the actual type when a pointer to a base class object or a virtual function is accessed when referenced, therefore, the member functions of the derived class can "Overwrite" The member functions of the base class.
Note that only functions with the same name and the same parameters with the virtual keyword can constitute virtual functions in the derived class and the base class respectively. This is also an important feature of the derived class.
In addition, it is associated with polymorphism, so it can be used only when class object pointers or references are used.
In a word, the overwrite function is a virtual function. Otherwise, the overwrite function is not a virtual function ~~ (If the base class and the inherited class function names, the returned values are the same [compilation cannot be performed if the return values are different]. If the base class uses virtual, whether or not the virtual keyword is added to the implementation of the inheritance class, the override relationship will still be formed)
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 definition of the function 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. Then 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 ....
Back to the hidden definition, we have mentioned that there are virtual keywords in the same name as the derived class and the base class respectively. The same Parameter Function constitutes the overwrite relationship, so the hidden relationship is only possible as follows:
1) It must be in the derived class and the base class respectively.
2) The name must be the same.
3) when the parameters are different, they do not constitute an overwrite relationship. Therefore, whether the parameter is a virtual function is no longer important.
When the parameters are the same, it depends on the virtual keyword. If the parameters are the same, the overwrite relationship is used. If the parameters are the same, the hidden relationship is used.
Many people cannot tell the difference between hiding and overwriting, because they all happen in the base class and the derived class. however, the most important difference between them is that the covered functions are polymorphism. Only functions in vtbl can constitute the "Overwrite" relationship, while hidden functions are general functions, polymorphism is not supported and has been determined in the compilation phase.