[英]Overloading virtual functions in two different interfaces
我遇到一個問題,即我有一個界面,該界面的某些部分按模板是有意義的,而某些部分則應為非模板而有意義。 我處於重構的中間,我將其拆分為兩個不同的接口,其中更具體的(模板化的)繼承自另一個接口。 例如,說我有一個接口IArray
,它將包含諸如Size()
類的函數,因為它並不關心數據類型。 但是我還有另一個接口ITArray<DataType>
,它具有類似DataType GetElement(...)
。 (這只是為了說明,不要對我大喊使用std :: vector)。 我進行此重構的原因是,非模板接口的使用方很多,他們希望不必編寫不需要模板類型的接受模板接口的模板函數(通常extern "C"
東西)
問題是我有重載的函數同時出現在兩個接口中,但是由於某種原因我無法解析基類函數。 這是我匯總的一個簡單示例,未在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 <>中,而無需手動將其放置在其中? 我看到維護的噩夢即將到來。
您是否嘗試過添加using
指令? IA::X
被A::X
隱藏。
class A : ...
{
public:
using IA::X;
virtual void X(DataType d) = 0;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.