为什么来自直接父的函数被调用,而不是来自祖父类。这叫做函数覆盖吗? [英] Why the function from immediate parent is called and not from grandparent class. Is this called function overriding?

查看:131
本文介绍了为什么来自直接父的函数被调用,而不是来自祖父类。这叫做函数覆盖吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class A
{
public:
void test ()
{
cout<<"In A";
}
};
class B :public A
{
public:
void test ()
{
cout<<"In B";
}
};
class C : public B
{

public:
int c;
};
int main()
{
C c;
c.test();
}

The result is: In B...


推荐答案

不,它不是重写,而是隐藏原来的方法。

No, it is not overriding, but rather hiding the original method.

C ++中的多态行为仅限于声明 virtual ,并且类方法中该方法的每个实现称为方法的 override

Polymorphic behavior in C++ is restricted to the methods declared virtual, and each implementation of that method in a class hierarchy is called an override of the method.

struct base {
   virtual void foo();
   void bar();
};
struct derived : base {
   virtual void foo();
   void bar();
};

在示例 base :: foo derived :: foo 是覆盖。当通过 base 类型的指针或引用使用类型的派生类时,将调用最终的覆盖方法(层次结构中最低的一个:在这种情况下 derived :: foo )。这里的重点是调用是通过指针或引用base:

In the example base::foo and derived::foo are overrides. When you use a class of type derived through a pointer or reference of type base, the final override method will be called (the lowest one in the hierarchy: in this case derived::foo). The important point here is that the call is through a pointer or reference to base:

void test() {
   derived d;
   base &b = d;
   b.foo(); // derived::foo() <- final overrider
   b.bar(); // base::bar()    <- non-virtual, no override
   d.bar(); // derived::bar() <- hides the method in base
}

bar (或在你的情况下)的情况下发生的是,当编译器发现调用 d.bar()它需要确定调用什么方法。要找到该方法,它将首先在派生的声明中找到 derived :: bar() base :: bar()无关,它将使用该方法而不检查类层次结构中的较高级别。如果您需要在层次结构中调用更高的方法,您可以通过获取对较高类型的引用或完全限定要调用的方法来实现。

What happens in the case of bar (or in your case) is that when the compiler finds the call d.bar() it needs to determine what method to call. To locate the method it will first look inside derived declaration and it will find derived::bar() (which is unrelated to base::bar()) and it will use that method without checking higher in the class hierarchy. If you need to call the method higher in the hierarchy you can do it by either getting a reference to the higher type or fully qualifying the method you want to call.

注意隐藏不仅发生在签名完全匹配时,而且在编译器找到具有相同名称的方法的所有情况下:

Note that hiding does not only happen when the signature matches completely, but in all cases where the compiler finds a method with the same name:

struct base {
   void bar();
};
struct derived : base {
   void bar( int );
};
void test() {
   derived d;
   base & b;

   b.bar();    // ok: base::bar()
   d.bar(1);   // ok: derived::bar(int)
   //b.bar(1); // error: base has no bar method that takes an integer
   //d.bar();  // error: derived::bar takes an integer, base::bar is hidden
   d.base::bar(); // ok: fully qualifying tells the compiler where to look
}

这篇关于为什么来自直接父的函数被调用,而不是来自祖父类。这叫做函数覆盖吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆