dolphin wrote:
Hi everyone . I am confused about the different between override
overload and hide.
May be I have a wrong opinion, I always think that hide is very
similar with override.
Both of them call the function of derived class,do not care the
function in the base class.
A derived class's function overrides a base class function if the signature
is the same and it's declared virtual in the base class. If you call it
through a pointer or a reference to the base class, the function in the
derived class is called. It "overrides" the one in the base class.
Hiding only comes into play if you call a non-virtual function through a
pointer or reference or directly with an object of the derived class. A
derived class' function hides all base class functions with the same name.
Maybe a piece of example code helps:
#include <iostream>
class Base
{
public:
virtual void foo() { std::cout << "Base::foo\ n"; }
void foo(int) { std::cout << "Base::foo(int) \n"; }
};
class Derived : public Base
{
public:
void foo() { std::cout << "Derived::foo\n "; }
};
int main()
{
Base b;
b.foo(); // Base's foo() is called
b.foo(3); // Base's foo(int) is called
Derived d;
d.foo(); // Derived's foo() is called. The one in Base is hidden
d.foo(3); // error, that function is also hidden
Base &ref = d;
ref.foo(); // Derived's foo() is called. It overrides the Base version
ref.foo(3); // Base's foo(int) is called
}