std :: vector类型的引用无效初始化

Con*_*chi 4 c++ polymorphism pointers interface

这是错误:

DummyService.hpp:35:错误:无效协变返回类型的 '虚拟的std ::矢量<ResourceBean*,性病::分配器<ResourceBean*>>&DummyService ::列表(常量的std :: string&)'

class Bean {
public:
    typedef std::string Path;
    virtual ~Bean() {};
    virtual const Path& getPath() = 0;
    virtual const std::string& getName() = 0;
protected:
    Bean();
};

class ResourceBean: public Bean {
public:
    ResourceBean(const Path& path, const std::string& contents) :
            _path(path), _contents(contents) {
    }
    virtual ~ResourceBean() { }
    virtual const Path& getPath();
    virtual void setPath(const Path& path);
    virtual const std::string& getName();
    virtual void setName(const std::string& name);
private:
    Path _path;
    std::string _name;
};
Run Code Online (Sandbox Code Playgroud)

上述Bean类是数据表示,它们由两个不同的层使用.一层使用该Bean接口,只是为了访问数据的getter.它ResourceBean由数据访问对象(DAO)类访问,它从数据库中获取数据(例如),并填写ResourceBean.

DAO的一个职责是列出给定某条路径的资源:

class Service {
protected:
    /*
     * Service object must not be instantiated (derived classes must be instantiated instead). Service is an interface for the generic Service.
     */
    Service();

public:
    virtual std::vector<Bean*>& list(const Bean::Path& path) = 0;
    virtual ~Service();
};

class DummyService: public Service {
public:
    DummyService();
    ~DummyService();

    virtual std::vector<ResourceBean*>& list(const ResourceBean::Path& path);
};
Run Code Online (Sandbox Code Playgroud)

我认为这个问题与std::vector<ResourceBean*>编译器不理解Bean实际上是基类的事实有关ResourceBean.

有什么建议?我读过一些类似的主题,但有些解决方案在我的案例中不起作用.如果我错过了什么,请指出.先感谢您.

小智 6

std::vector<Bean*>并且std::vector<ResourceBean*>是两种完全不同的类型,它们不能相互转换.你根本无法在C++中做到这一点,而应该坚持使用指向基类的指针向量.另外,正因为如此,让这个函数virtual没有按照你的想法做 - 不同的返回类型使它成为一个不同的方法签名,所以不是重载方法,而是引入一个新的虚方法.此外,不需要为抽象类(Service)保护构造函数,因为无论如何都无法创建抽象类的实例.我写的是这样的:

#include <string>
#include <vector>
#include <iostream>

class Bean {
  public:
    typedef std::string Path;

    Bean() {}
    virtual ~Bean() {}

    virtual void say() const
    {
        std::cout << "I am a bean!\n";
    }
};

class ResourceBean : public Bean {
  public:
    ResourceBean() {}
    virtual ~ResourceBean() {}

    virtual void say() const
    {
        std::cout << "I am a resource bean!\n";
    }
};

class Service {
public:
    Service() {}
    virtual ~Service() {}

    virtual std::vector<Bean*>& list(const Bean::Path &path) = 0;
};

class DummyService: public Service {
  public:
    DummyService()
    {
        for (int i = 0; i < 5; ++i)
            beans_.push_back(new ResourceBean());
    }

    virtual ~DummyService() {}

    virtual std::vector<Bean *>& list(const Bean::Path &path)
    {
        return beans_;
    }

  private:
    std::vector<Bean*> beans_;
};

int main()
{
    DummyService s;
    std::vector<Bean *>& l = s.list("some path");
    for (std::vector<Bean *>::iterator it = l.begin(), eit = l.end();
         it != eit; ++it)
    {
        Bean *bean = *it;
        bean->say();
    }
}
Run Code Online (Sandbox Code Playgroud)