重载与覆盖 [英] overloading vs overriding
问题描述
我对这两个术语有些困惑,很高兴澄清一些疑问.
I am a little confused over the two terminologies and would be glad to get some doubts clarified.
据我了解,function overloading
表示在同一类中具有多个具有相同名称的方法,但具有不同数量的参数,不同类型的参数或参数序列,而与返回类型无关,而这对已损坏的名称无效功能.
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)....."
Function Overriding
与虚函数有关,它们具有相同的方法签名(在基类中声明为虚拟),并为子类中的实现所覆盖.
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 scenario, 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;
}
在上述情况下,可以说:
基类OR
中派生类overrides
中的方法
基类的派生类overloads
方法中的方法
In the above scenario, What can be said:
The method in derived class overrides
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?
我认为应该是overrides
,但只需要澄清一下,因为我碰巧记得专门用于虚拟函数的术语覆盖".
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.
推荐答案
在这种情况下都不是.派生类方法隐藏类方法.
In this case neither. The derived-class method hides the base-class method.
这篇关于重载与覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!