0

我正在尝试使用基于策略的设计来概括我的类,并且 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;

有两个问题我不太明白:

  1. 即使函数签名完全相同,编译器似乎也不认为AccessPolicy< ReturnType >::implementation< DataClass >...是一个实现 。AccessPolicy< ReturnType >::interface...
  2. 即使它们都有不同的参数,编译器也无法解析我正在调用的 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;
}
4

2 回答 2

1

struct testImplementation是从struct testInterface它自己继承的,struct GetByNameInterface它定义了一个virtual std::string operator[](std::string ) = 0;

既没有testInterface也没有testImplementation为此虚拟定义覆盖,testImplementation抽象结构也是如此。

您从另一个与前一个没有关系但定义相同的类继承这一事实对您operator没有帮助。您必须在具体实现和抽象接口之间的层次结构中实现您的方法,而不是在侧类上。

于 2014-01-26T10:13:32.790 回答
0

为了做到这一点,你implementation的类必须从你的interface类继承,这种方式的实现将被接受,但正如之前所说,从一个具有抽象函数实现的不相关类继承,并不意味着实现的函数应该被视为抽象的实现功能

于 2014-01-26T10:34:28.977 回答