c ++问题与继承类中的函数重载 [英] c++ issue with function overloading in an inherited class

查看:111
本文介绍了c ++问题与继承类中的函数重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个noob问题,对不起。我最近遇到一个奇怪的问题,当试图搞砸一些高级的东西在c ++,函数重载和继承。

This is possibly a noob question, sorry about that. I faced with a weird issue recently when trying to mess around with some high level stuff in c++, function overloading and inheritance.

我会显示一个简单的例子,只是显示问题;

I'll show a simple example, just to demonstrate the problem;

有两个类, classA classB ,如下所示;

There are two classes, classA and classB, as below;

class classA{
    public:
        void func(char[]){};    
};

class classB:public classA{ 
    public:
        void func(int){};
};

根据我知道 classB 有两个 func(..)函数,由于不同的参数重载。

According to what i know classB should now posses two func(..) functions, overloaded due to different arguments.

方法;

int main(){
    int a;
    char b[20];
    classB objB;
    objB.func(a);    //this one is fine
    objB.func(b);    //here's the problem!
    return 0;
}

它提供错误作为方法 void func []){}; 在超类中, classA ,在派生类中不可见, classB

It gives errors as the method void func(char[]){}; which is in the super class, classA, is not visible int the derived class, classB.

如何解决这个问题?是不是这是重载如何工作在C ++?我是新来的c ++,但在Java,我知道我可以利用这样的东西。

How can I overcome this? isn't this how overloading works in c++? I'm new to c++ but in Java, i know I can make use of something like this.

虽然我已经找到此主题,其中询问类似的问题,我认为这两种情况是不同的。

Though I've already found this thread which asks about a similar issues, I think the two cases are different.

推荐答案

所有你需要的是使用

class classB:public classA{ 
    public:
        using classA::func;
        void func(int){};
};

它不搜索基类 func 因为它已经在派生类中找到一个。 使用语句将其他重载放入同一范围,以便它可以参与重载解析。

It doesn't search the base class for func because it already found one in the derived class. The using statement brings the other overload into the same scope so that it can participate in overload resolution.

这篇关于c ++问题与继承类中的函数重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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