What is the different between override overload and hide?

D

dolphin

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.
I know I make something wrong here, but i dont kow what the wrong it
is? Could some one point out for me? sorry for my poor english.
 
R

Rolf Magnus

dolphin said:
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
}
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top