在两个不同的接口中重载虚拟函数

发布于 2024-09-26 17:15:04 字数 1312 浏览 0 评论 0原文

我有一个问题,我有一个界面,其中包含作为模板有意义的部分和作为非模板有意义的部分。我正在进行重构,将其分成两个不同的接口,其中更具体的接口(模板接口)继承另一个接口。例如,假设我有一个接口 IArray,它将包含诸如 Size() 之类的函数,因为它不关心数据类型。但我有另一个接口 ITArray ,它有类似 DataType GetElement(...) 的内容。 (这只是为了说明,不要对我大喊大叫使用 std::vector)。我进行此重构的原因是,有很多非模板化接口的使用者,如果他们不需要模板化类型(通常extern "C" stuff)

问题是我重载了两个接口中都出现的函数,但由于某种原因我无法解析基类函数。这是我整理的一个简单示例,未在 MSVC 中编译:

#include <iostream>

class IA
{
public:
        virtual void X()=0;
};

template <class DataType>
class ITA : public IA
{
public:
        //If I uncomment this line, it all works!
        //virtual void X()=0;
        virtual void X(DataType d)=0;
};

template <class DataType>
class A : public ITA<DataType>
{
public:
    void X()
    {
        std::cout << "In X" << std::endl;
    }

    void X(DataType d)
    {
        std::cout << "In X, d=" << d << std::endl;
    }
};

template <class DataType>
void DoSomething(ITA<DataType>& I, DataType d)
{
    I.X(); //MSVC can't resolve this since it's in IA, not ITA
    I.X(d);
}

int main(int argc, char* argv[])
{
    A<int> I;
    DoSomething(I,10);
}

是否有某种方法可以使 IA 的函数出现在 ITA<> 中?无需手动将它们放在那里?我看到面前的维护噩梦。

I have an issue where I have an interface that has parts that make sense as templated, and parts that make sense as not-templated. I'm in the middle of a refactor where I'm splitting that out into two different interfaces where the more specific (the templated one) inherits from the other one. E.g., say I have an interface IArray, this would contain functions like Size() since it doesn't care about the data type. But I'd have another interface ITArray<DataType> that would have something like DataType GetElement(...). (This is just for illustration, don't yell at me to use std::vector). The reason I'm doing this refactor is that there are a lot of consumers of the non-templated interface and they'd like to not have to write templated functions that accept a templated interface if they don't need the templated type (generally extern "C" stuff)

The problem is that I have overloaded functions that appear in both interfaces, but for some reason I can't resolve the base class functions. Here's a simple example that I put together that's not compiling in MSVC:

#include <iostream>

class IA
{
public:
        virtual void X()=0;
};

template <class DataType>
class ITA : public IA
{
public:
        //If I uncomment this line, it all works!
        //virtual void X()=0;
        virtual void X(DataType d)=0;
};

template <class DataType>
class A : public ITA<DataType>
{
public:
    void X()
    {
        std::cout << "In X" << std::endl;
    }

    void X(DataType d)
    {
        std::cout << "In X, d=" << d << std::endl;
    }
};

template <class DataType>
void DoSomething(ITA<DataType>& I, DataType d)
{
    I.X(); //MSVC can't resolve this since it's in IA, not ITA
    I.X(d);
}

int main(int argc, char* argv[])
{
    A<int> I;
    DoSomething(I,10);
}

Is there some way that I can make IA's functions appear in ITA<> without manually putting them there? I see a maintenance nightmare ahead of me.

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

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

发布评论

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

评论(1

卖梦商人 2024-10-03 17:15:04

您是否尝试过添加 using 指令? IA::XA::X 隐藏。

class A : ...
{
public:
    using IA::X;
    virtual void X(DataType d) = 0; 
};

Have you tried adding a using directive? IA::X is a hidden by A::X.

class A : ...
{
public:
    using IA::X;
    virtual void X(DataType d) = 0; 
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文