从模板类的多重继承
问题描述:
我想拥有一个可以存储各种对象的通用存储类,我不想使用存储所有对象的异构容器类。我想创建一个模板存储类,并创建一个继承一般的存储类从这种与不同类型的元类:从模板类的多重继承
template<typename Type>
struct SingleInterface
{
public:
SingleInterface() = default;
virtual ~SingleInterface() = default;
SingleInterface(const SingleInterface &) = default;
SingleInterface & operator=(const SingleInterface &) = default;
SingleInterface(SingleInterface &&) = default;
SingleInterface & operator=(SingleInterface &&) = default;
void get(const std::string & keyword, Type * ptr)
{
// implementation
}
};
我的一般类,如下:
class MutliInterface: public SingleInterface<double>, SingleInterface<int>
{
public:
MutliInterface() = default;
virtual ~MutliInterface() = default;
};
当我创建一个MutliInterface类,我得到以下错误:
MutliInterface interface;
double *v;
interface.get("test", v);
'get' is ambiguous '
答
揭露过载都用using
声明在派生类:
class MutliInterface: public SingleInterface<double>, SingleInterface<int>
{
public:
MutliInterface() = default;
virtual ~MutliInterface() = default;
using SingleInterface<double>::get;
using SingleInterface<int>::get;
};
这是明确的,因为的[class.member.lookup],基本上说,当一个函数get
的名字不在类MutliInterface
,一组候选立即发现函数是使用类的基类创建的。但是,函数必须是相同的,否则它是不明确的(在这里它们不相同)。
当我们在此处使用using
关键字时,我们将所有这些都缩短,并且两个重载都可以从派生类中立即访问。
未输入get方法解决歧义??? void get(const std :: string&keyword,Type * ptr) – apramc