调用隐藏基类方法的最佳方法 [英] Best way to call hidden base class method

查看:72
本文介绍了调用隐藏基类方法的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,"d.Foo()"引发编译器错误,声称函数Foo()不带0参数.基类中仍然存在具有该名称的0参数函数. "d.Base :: Foo()"行是可以接受的.

In the following code, "d.Foo()" throws a compiler error claiming that function Foo() does not take 0 arguments. Yet a 0-argument function with that name exists in the base class. The line "d.Base::Foo()" is acceptable.

我对学习一个模糊的记忆是,即使参数可能不同,在派生类中使用函数名称也会在基类中隐藏该名称的所有函数.我不记得为什么,也不记得避免该问题的最佳方法.我的解决方案是最好的,还是可以通过Base :: Foo()获得另一种方法?

I have a vague memory of learning that the use of a function name in a derived class hides all functions of that name in a base class, even though arguments may be different. I don't remember why, nor do I remember the best way to avoid that problem. Is my solution best, or is there another way to get at Base::Foo()?

非常感谢!

RobR

// Override.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
class Base
{
public :
    void Foo()
    {
    }
};

class Derived : public Base
{
public: 
    void Foo(int x)
    {
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Derived d;
    d.Foo();
    d.Base::Foo();

    return 0;
}

推荐答案

您可以将Derived::Foo()定义为:

class Derived : public Base {
public:
    void Foo() { Base::Foo(); }
};

这篇关于调用隐藏基类方法的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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