虚函数重定义隐藏了另一个基类的同名其他重载函数

发布于 2024-09-24 02:48:45 字数 2726 浏览 0 评论 0原文

好的,我正在使用虚函数、重载函数和多重继承。当然,结果并不好。

场景:类 base1 有一个虚函数,需要由其子类指定。 衍生类从两个父级base1base2派生,并且应该使用base2的现有功能来定义base1 的虚函数。

这很好,但当然很尴尬。动机是我无法更改类 base2 ,并且已经大量投入的现有接口具有同名的 base1base2 类函数。没关系,base1 中没有实现任何内容,它应该只是重定向到 base2

我的问题出现是因为 base2 有几个与相关虚函数同名的重载函数。所有其他重载版本在编译时基本上都是隐藏的。

这是一个小演示代码。

// this version does not compile, overloaded samenameFunc(int, int) cannot be found in the derived class.
#include <iostream>
using namespace std;

class base1 {
public:
    virtual void samenameFunc(int scratch) { cout << "from base1: " << scratch << std::endl; }
};

class base2 {
public:
    void samenameFunc(int scratch) { cout << "samenameFunc from base2: " << scratch << std::endl; }
    void samenameFunc(int scratch, int foo)  { cout << "samenameFunc(2 args) from base2: " << scratch << ", " << foo << std::endl; }
};

class derived : public base1, public base2 {
public:
    void samenameFunc(int scratch) { base2::samenameFunc(scratch); }// { cout << "from derived: " << scratch << std::endl; }
};

int main()
{
    derived d;

    d.samenameFunc(66);
    d.samenameFunc(77, 88);
    return 0;
}

编译器错误:

$ g++ inheritance_overload_tester.cc 
inheritance_overload_tester.cc: In function ‘int main()’:
inheritance_overload_tester.cc:26: error: no matching function for call to ‘derived::samenameFunc(int, int)’
inheritance_overload_tester.cc:18: note: candidates are: virtual void derived::samenameFunc(int)

如果重载的 samenameFunc(int, int) 被重命名,我可以按预期编译和运行。但请记住,我实际上无法更改 base2。

// this version does compile
#include <iostream>
using namespace std;

class base1 {
public:
    virtual void samenameFunc(int scratch) { cout << "from base1: " << scratch << std::endl; }
};

class base2 {
public:
    void samenameFunc(int scratch) { cout << "samenameFunc from base2: " << scratch << std::endl; }
    void anotherFunc(int scratch, int foo)  { cout << "anotherFunc(2 args) from base2: " << scratch << ", " << foo << std::endl; }
};

class derived : public base1, public base2 {
public:
    void samenameFunc(int scratch) { base2::samenameFunc(scratch); }// { cout << "from derived: " << scratch << std::endl; }
};

int main()
{
    derived d;

    d.samenameFunc(66);
    d.anotherFunc(77, 88);
    return 0;
}

如果在现有的演示文稿中无法解决此问题,有人可以建议使用不同的对象继承模型来解决我的问题吗?

Ok, I'm using virtual functions, overloaded functions, and multiple inheritance. Of course this doesn't turn out well.

The scenario: Class base1 has a virtual function that needs to be specified by its child.
Class derived derives from two parents base1 and base2, and should use base2's existing functionality to define base1's virtual function.

This is ok, but awkward of course. The motivation is I cannot change the class base2 and the existing interface already heavily invested in has base1 and base2 class functions of the same name. This is ok, nothing is implemented in base1, it should just redirect to base2.

My problem arises because base2 has several overloaded functions of the same name as the virtual function in question. All of the other overloaded versions become essentially hidden at compile time.

Here's a small demonstration code.

// this version does not compile, overloaded samenameFunc(int, int) cannot be found in the derived class.
#include <iostream>
using namespace std;

class base1 {
public:
    virtual void samenameFunc(int scratch) { cout << "from base1: " << scratch << std::endl; }
};

class base2 {
public:
    void samenameFunc(int scratch) { cout << "samenameFunc from base2: " << scratch << std::endl; }
    void samenameFunc(int scratch, int foo)  { cout << "samenameFunc(2 args) from base2: " << scratch << ", " << foo << std::endl; }
};

class derived : public base1, public base2 {
public:
    void samenameFunc(int scratch) { base2::samenameFunc(scratch); }// { cout << "from derived: " << scratch << std::endl; }
};

int main()
{
    derived d;

    d.samenameFunc(66);
    d.samenameFunc(77, 88);
    return 0;
}

And the compiler errors:

$ g++ inheritance_overload_tester.cc 
inheritance_overload_tester.cc: In function ‘int main()’:
inheritance_overload_tester.cc:26: error: no matching function for call to ‘derived::samenameFunc(int, int)’
inheritance_overload_tester.cc:18: note: candidates are: virtual void derived::samenameFunc(int)

If the overloaded samenameFunc(int, int) is renamed, I can compile and function as expected. But remember, I can't actually change base2.

// this version does compile
#include <iostream>
using namespace std;

class base1 {
public:
    virtual void samenameFunc(int scratch) { cout << "from base1: " << scratch << std::endl; }
};

class base2 {
public:
    void samenameFunc(int scratch) { cout << "samenameFunc from base2: " << scratch << std::endl; }
    void anotherFunc(int scratch, int foo)  { cout << "anotherFunc(2 args) from base2: " << scratch << ", " << foo << std::endl; }
};

class derived : public base1, public base2 {
public:
    void samenameFunc(int scratch) { base2::samenameFunc(scratch); }// { cout << "from derived: " << scratch << std::endl; }
};

int main()
{
    derived d;

    d.samenameFunc(66);
    d.anotherFunc(77, 88);
    return 0;
}

If this can't be fixed in the existing presentation, can someone suggest a different object inheritance model to solve my problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼中杀气 2024-10-01 02:48:45

衍生类的定义中,您可以添加一个using声明以使base2中的函数可见:

using base2::samenameFunc;

In the definition of the derived class, you can add a using declaration to make the function from base2 visible:

using base2::samenameFunc;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文