为什么要调用来自直接父级的函数而不是祖父级类。这被称为函数覆盖吗?

时间:2010-03-20 13:44:03

标签: c++

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...

1 个答案:

答案 0 :(得分:5)

不,它不是覆盖,而是隐藏原始方法。

C ++中的多态行为仅限于声明为virtual的方法,并且类层次结构中该方法的每个实现都称为方法的覆盖

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

在示例中base::fooderived::foo是覆盖。当您通过类型为derived的指针或引用使用类型base时,将调用最终重写方法(层次结构中最低的方法:在本例中为derived::foo)。这里重要的一点是,调用是通过指针或对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声明内部,它将找到derived::bar()(与base::bar()无关)并且它将使用该方法而不检查类层次结构中的更高级别。如果您需要在层次结构中调高方法,可以通过获取更高类型的引用或完全限定要调用的方法来实现。

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

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
}