我正在尝试使用基于策略的设计来推广我的类,并且似乎gcc没有看到在基类中实现的纯虚函数的实现。这是一个例子:
#include <iostream>
template <typename ReturnValue, template <typename> class... AccessPolicies>
struct testInterface : public AccessPolicies< ReturnValue >::interface...
{
};
template <typename DataClass, typename ReturnValue, template <typename> class... AccessPolicies>
struct testImplementation : public DataClass,
public testInterface< ReturnValue, AccessPolicies... >,
public AccessPolicies< ReturnValue >::template implementation< DataClass >...
{
};
template < typename ReturnValue >
struct GetByIndex;
template <>
struct GetByIndex< std::string >
{
class interface
{
public:
virtual std::string operator[](size_t ) = 0;
protected:
virtual ~interface() = default;
};
template <class DataClass>
class implementation
{
public:
virtual std::string operator[](size_t )
{
return "test by index";
}
protected:
virtual ~implementation() = default;
};
};
template < typename ReturnValue >
struct GetByName;
template <>
struct GetByName< std::string >
{
class interface
{
public:
virtual std::string operator[](std::string ) = 0;
protected:
virtual ~interface() = default;
};
template <class DataClass>
class implementation
{
public:
virtual std::string operator[](std::string )
{
return "test by string";
}
protected:
virtual ~implementation() = default;
};
};
struct data
{
};
int main()
{
testImplementation< data, std::string, GetByIndex, GetByName> test;
testInterface< std::string, GetByIndex, GetByName >& Test = test;
std::cout << Test[5] << std::endl;
return 0;
}
我得到的错误是:
..\nienazwany\main.cpp: In function 'int main()':
..\nienazwany\main.cpp:78:67: error: cannot declare variable 'test' to be of abstract type 'testImplementation<data, std::basic_string<char>, GetByIndex, GetByName>'
testImplementation< data, std::string, GetByIndex, GetByName> test;
^
..\nienazwany\main.cpp:10:8: note: because the following virtual functions are pure within 'testImplementation<data, std::basic_string<char>, GetByIndex, GetByName>':
struct testImplementation : public DataClass,
^
..\nienazwany\main.cpp:53:29: note: virtual std::string GetByName<std::basic_string<char> >::interface::operator[](std::string)
virtual std::string operator[](std::string ) = 0;
^
..\nienazwany\main.cpp:26:29: note: virtual std::string GetByIndex<std::basic_string<char> >::interface::operator[](size_t)
virtual std::string operator[](size_t ) = 0;
^
..\nienazwany\main.cpp:81:24: error: request for member 'operator[]' is ambiguous
std::cout << Test[5] << std::endl;
^
..\nienazwany\main.cpp:53:29: note: candidates are: virtual std::string GetByName<std::basic_string<char> >::interface::operator[](std::string)
virtual std::string operator[](std::string ) = 0;
^
..\nienazwany\main.cpp:26:29: note: virtual std::string GetByIndex<std::basic_string<char> >::interface::operator[](size_t)
virtual std::string operator[](size_t ) = 0;
有两个问题我不太了解:
AccessPolicy< ReturnType >::implementation< DataClass >...
是AccessPolicy< ReturnType >::interface...
的实现,即使函数签名完全相同。任何想法为什么会发生这种情况?
我的猜测是,即使我直接从“接口”和“实现”继承,成员函数也会以某种方式结束于不同的命名空间。如果这是正确的,我该如何解决这个问题?
编辑:根据请求添加了以上剥去模板的示例
#include <iostream>
class GetByIndexInterface
{
public:
virtual std::string operator[](size_t ) = 0;
protected:
virtual ~GetByIndexInterface() = default;
};
class GetByIndexImplementation
{
public:
virtual std::string operator[](size_t )
{
return "test by index";
}
protected:
virtual ~GetByIndexImplementation() = default;
};
class GetByNameInterface
{
public:
virtual std::string operator[](std::string ) = 0;
protected:
virtual ~GetByNameInterface() = default;
};
class GetByNameImplementation
{
public:
virtual std::string operator[](std::string )
{
return "test by string";
}
protected:
virtual ~GetByNameImplementation() = default;
};
struct data
{
};
struct testInterface : public GetByIndexInterface,
public GetByNameInterface
{
};
struct testImplementation : public data,
public testInterface,
public GetByIndexImplementation,
public GetByNameImplementation
{
};
int main()
{
testImplementation test;
testInterface& Test = test;
std::cout << Test[5] << std::endl;
return 0;
}
答案 0 :(得分:1)
您struct testImplementation
继承自struct testInterface
本身,继承自struct GetByNameInterface
,定义了virtual std::string operator[](std::string ) = 0;
testInterface
和testImplementation
都没有定义此虚拟的覆盖,因此testImplementation
是一个抽象结构。
您继承自另一个与前一个类无关但定义相同operator
的类的事实对您没有帮助。您必须在具体实现和抽象接口之间的层次结构中实现您的方法,而不是在侧类上。
答案 1 :(得分:0)
为了做到这一点,你implementation
类必须从你的interface
类继承,这种方式实现将被接受,但正如在从具有抽象函数实现的不相关类继承之前所说的那样,并不意味着实现的功能应被视为抽象功能的实现