在两个不同的接口中重载虚拟函数
我有一个问题,我有一个界面,其中包含作为模板有意义的部分和作为非模板有意义的部分。我正在进行重构,将其分成两个不同的接口,其中更具体的接口(模板接口)继承另一个接口。例如,假设我有一个接口 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过添加
using
指令?IA::X
被A::X
隐藏。Have you tried adding a
using
directive?IA::X
is a hidden byA::X
.