overloading vs overriding
I am a little confused over the two terminologies and would be glad to get some doubts clarified.
As I understand function overloading
means having multiple methods in the same class with same name but either with different number of arguments, different types of arguments or sequence of arguments irrespective of the return type which has no effect in mangled name of the functions.
Does the above definition also include "....in the same class or across related classes(related through inheritance)....."
And Function Overriding
is related to virtual functions, same method signature(declared virtual in Base class) and overridden for implementation in Sub Classes.
I was wondering at a scenari开发者_开发百科o, following is the code:
#include <iostream>
class A
{
public:
void doSomething(int i, int j)
{
cout<<"\nInside A::doSomething\n";
}
};
class B: public A
{
public:
void doSomething(int i, int j)
{
cout<<"\nInside B::doSomething\n";
}
};
int main()
{
B obj;
obj.doSomething(1,2);
return 0;
}
In the above scenario, What can be said:
The method in derived classoverrides
method in Base class OR
The method in derived class overloads
method in Base Class
Does Overloading apply across class scopes and does overriding term doesn't necessarily apply to virtual functions?
I think it should be overrides
, but just need the clarification because i happen to remember the term overriding being used specifically with virtual functions.
- hiding is when a definition in a scope is not accessible due to a declaration in a nested scope or a derived class (3.3.7/1).
A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived class.
- overriding is when a virtual member is replaced in a derived class (see 10.3/2)
If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name and same parameter list as Base::vf is declared, then Derived::vf is also virtual an it overrides Base::vf.
- overloading is when several declarations coexist for the same name in the same scope (13/1)
When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded.
- related, there is also the possibility of replacing operator new and delete from the standard library by one's own implementation (18.4.1.1/2, 18.4.1.1/6, 18.4.1.1/11, 18.4.1.2)
So this is clearly a case of hiding.
In this case neither. The derived-class method hides the base-class method.
Function overloading is when you have several functions which differ in their parameter list or, if they are member functions, in their const
/volatile
qualification. (In some other languages you can also overload based on the return type, but C++ doesn't allow that.)
Examples:
void f(int);
void f(char);
class some_class {
void g();
void g() const;
};
Function overriding is when you redefine a base class function with the same signature. Usually this only makes sense if the base class function is virtual, because otherwise the function to be called (base or derived class' version) is determined at compile-time using a reference's/pointer's static type.
Examples:
class base {
void f();
virtual void g();
};
class derived : public base {
void f();
void g();
};
Function hiding is when you define a function ina derived class (or an inner scope) that has a different parameter list than a function with the same name declared in a base class (or outer scope). In this case the derived class' function(s) hides the base class function(s). You can avoid that by explicitly bringing the base class function(s) into the derived class' scope with a using
declaration.
Examples:
class base {
void f(int);
void f(char);
};
class derived1 : public base {
void f(double);
};
void f()
{
derived1 d;
d.f(42); // calls derived1::f(double)!
}
class derived2 : public base {
using base::f; // bring base class versions into derived2's scope
void f(double);
};
void g()
{
derived2 d;
d.f(42); // calls base::f(int)!
}
Just in case it's unclear: Based on these definitions, I'd call the scenario in question here overriding.
Overloading is the process of defining multiple methods with identical names but different signatures; Overriding is when a function in a child class has an identical signature to a virtual function in a parent class.
class Test {
// Test::func is overloaded
virtual void func(int x);
virtual void func(double y);
};
class Child : public Test {
// Child::func overrides Test::func
virtual void func(int x);
};
精彩评论