【问题标题】:invalid initialization of reference of type std::vectorstd::vector 类型引用的无效初始化
【发布时间】:2012-12-18 23:06:23
【问题描述】:

这是错误:

DummyService.hpp:35:错误:'virtual 的协变返回类型无效 std::vector >& DummyService::list(const 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;
};

上面的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);
};

我认为问题与std::vector&lt;ResourceBean*&gt; 中的编译器不理解Bean 实际上是ResourceBean 的基类有关。

有什么建议吗?我已经阅读了一些类似的主题,但有些解决方案在我的情况下不起作用。请指出我是否遗漏了什么。提前谢谢你。

【问题讨论】:

  • 在继续之前,请注意您的代码不是类型安全的(它是用来编译的)。 std::vector&lt;ResourceBean*&gt; 不是 std::vector&lt;Bean*&gt; 的有效子类型(在一般 LSP 意义上),因为 std::vector&lt;ResourceBean*&gt; 不支持 std::vector&lt;Bean*&gt; 所做的所有操作(例如,您可以将任何 Bean*放入std::vector&lt;Bean*&gt;,而您只能将ResourceBean*放入std::vector&lt;ResourceBean*&gt;)。
  • 感谢您的回复。我试图理解您为什么说“ std::vector 不支持 std::vector 所做的所有操作”。另外,当您说“您可以将任何 Bean* 放入 std::vector 中,而您只能将 ResourceBean* 放入 std::vector”时,您的意思是您可以放入任何 Bean*(包括派生类的 *)?
  • 是的。例如,您可以FooBean* 放入std::vector&lt;Bean*&gt;(其中FooBeanBean 的某个子类),但您不能放入@ 987654342@ 转为std::vector&lt;ResourceBean*&gt;。如果某些代码使用Service::list 并希望引用std::vector 可以放入FooBean*,那么DummyService::list 返回的std::vector&lt;ResourceBean*&gt; 将不够用。
  • 啊我明白了,谢谢你指出这一点。

标签: c++ pointers interface polymorphism


【解决方案1】:

std::vector&lt;Bean*&gt;std::vector&lt;ResourceBean*&gt; 是两个完全不同的类型,它们不能相互转换。您根本无法在 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();
    }
}

【讨论】:

  • 有道理,但是 Bean 类不能用数据填充(因为它只包含 getter)。有什么方法可以在 DummyService 的 list 方法中返回 std::vector& ,但在该方法中从 ResourceBean 转换为 Bean?
  • @user1570742: ResourceBean,当从Resource 继承时,可以隐式转换为Resource,它的基类。我已经编辑了我的答案并为此添加了一些示例。
  • 感谢您抽出宝贵时间对此进行解释。现在完美运行。
猜你喜欢
  • 2012-08-02
  • 2012-08-07
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
  • 2013-06-06
  • 2019-01-16
  • 2015-05-22
相关资源
最近更新 更多