我正在尝试使用基于策略的设计来概括我的类,并且 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...
- 即使它们都有不同的参数,编译器也无法解析我正在调用的 operator[],而我显然正在调用 size_t (数字不能隐式转换为字符串)。
任何想法为什么会发生这种情况?
我的猜测是,即使我直接从“接口”和“实现”继承,成员函数也会以某种方式最终出现在不同的命名空间中。如果这是正确的,我该如何解决这个问题?
编辑:根据请求添加了上面的示例,去掉了模板
#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;
}