Re: What is the different between override overload and hide?
On 2007-11-02 10:41, Rolf Magnus wrote:
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"; }
};
Just like to point out that overloading is when you have two functions
with different signatures but the same name (as above) in the same
scope. The last part about scope is important.
struct B
{
void foo();
void foo(int);
void bar();
};
struct D : public B
{
void bar(int);
};
Here foo is overloaded in B, but bar(int) in D is not an overload of of
bar() in B since they are in different scopes. As Rolf Magnus explained
bar(int) in D hides bar() in B.
--
Erik Wikstr??m