祖父母重载函数在孩子 [英] Grandparent overloaded function in child

查看:123
本文介绍了祖父母重载函数在孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要了解为什么C ++不允许访问Child中的祖父项重载函数,如果在父项中声明了任何重载的函数。考虑下面的例子:

I need to understand why C++ don't allow to access Grandparent overloaded functions in Child if any of the overloaded function is declared in Parent. Consider the following example:

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
    }
};

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

Here, two functions foo() and foo(int) are overloaded functions in Grandparent. But foo(int) is not accessible since foo() is declared in Parent (doesn't matter if it declared is public or private or protected). However, test() is accessible, which is right as per OOP.

我需要知道此行为的原因。

I need to know the reason of this behavior.

推荐答案

原因是方法隐藏

当您在派生类中声明一个具有相同名称的方法时,将隐藏具有该名称的基类方法。

When you declare a method with the same name in a derived class, base class methods with that name are hidden. The full signature doesn't matter (i.e. cv-qualifiers or argument list).

如果您明确要允许调用,可以使用

If you explicitly want to allow the call, you can use

using grandparent::foo;

里面 parent

这篇关于祖父母重载函数在孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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