祖父母在孩子身上重载了功能

Jaw*_*tar 9 c++ overriding overloading

我需要理解为什么C++不允许在Child中访问Grandparent重载函数,如果在Parent中声明了任何重载函数.请考虑以下示例:

class grandparent{
public:
    void foo();
    void foo(int);
    void test();
};

class parent : public grandparent{
public:
    void foo();
};

class child : public parent{
public:
    child(){
        //foo(1); //not accessible
        test();   //accessible
    }
};
Run Code Online (Sandbox Code Playgroud)

这里,两个函数foo()和foo(int)是Grandparent中的重载函数.但是foo(int)是不可访问的,因为foo()在Parent中声明(如果声明它是public或private或protected,则无关紧要).但是,test()是可访问的,根据OOP是正确的.

我需要知道这种行为的原因.

Luc*_*ore 10

原因是方法隐藏.

在派生类中声明具有相同名称的方法时,将隐藏具有该名称的基类方法.完整签名无关紧要(即cv-qualifiers或参数列表).

如果您明确要允许通话,则可以使用

using grandparent::foo;
Run Code Online (Sandbox Code Playgroud)

在里面parent.