我实现了一个小类层次结构,需要从工厂类实例化子类。我的课程ConcreteProduct
来自AbstractProduct
,我的课程ConcreteFactory
来自AbstractFactory
。我的代码如下:
#ifndef FACTORY_H
#define FACTORY_H
#include <cstddef>
#include <vector>
#include <memory>
template <typename DATATYPE, typename RETURNTYPE>
class AbstractProduct {
protected:
DATATYPE data;
std::size_t count;
public:
explicit AbstractProduct(DATATYPE _data, std::size_t _count):
data(_data),
count(_count) {}
virtual void doSomething() = 0;
};
template <typename DATATYPE, typename RETURNTYPE>
class ConcreteProduct: public AbstractProduct<DATATYPE, RETURNTYPE> {
public:
ConcreteProduct(DATATYPE _data, std::size_t _count):
AbstractProduct<DATATYPE, RETURNTYPE>(_data, _count) {}
virtual void doSomething() override;
};
template <typename DATATYPE, typename RETURNTYPE>
class AbstractFactory {
public:
virtual
std::vector<std::unique_ptr<AbstractProduct<DATATYPE, RETURNTYPE>>>
createProducts(DATATYPE _data) = 0;
};
template <typename DATATYPE, typename RETURNTYPE>
class ConcreteFactory: public AbstractFactory<DATATYPE, RETURNTYPE> {
public:
virtual
std::vector<std::unique_ptr<ConcreteProduct<DATATYPE, RETURNTYPE>>>
createProducts(DATATYPE _data) override;
};
#endif
#include "factory.h"
template class ConcreteProduct<int*, int*>;
template class ConcreteFactory<int*, int*>;
template <typename DATATYPE, typename RETURNTYPE>
void ConcreteProduct<DATATYPE, RETURNTYPE>::doSomething() {
int a = 5+3;
}
template <typename DATATYPE, typename RETURNTYPE>
std::vector<std::unique_ptr<ConcreteProduct<DATATYPE, RETURNTYPE>>> ConcreteFactory<DATATYPE, RETURNTYPE>::createProducts(DATATYPE _data) {
std::vector<std::unique_ptr<ConcreteProduct<DATATYPE, RETURNTYPE>>> result;
for(std::size_t i = 0; i < 5; ++i) {
result.push_back(std::make_unique<ConcreteProduct<DATATYPE, RETURNTYPE>>(_data, 1));
}
return result;
}
#include "factory.h"
int main() {
int* data = new int[20];
ConcreteFactory<int*, int*> factory;
std::vector<std::unique_ptr<ConcreteProduct<int*, int*>>> products = factory.createProducts(data);
return 0;
}
当我尝试使用g++ -o factory -std=c++14 main.cpp factory.cpp
(g ++ 4.9.2)编译时,我收到此错误:
In file included from main.cpp:1:0:
factory.h: In instantiation of ‘class ConcreteFactory<int*, int*>’:
main.cpp:7:33: required from here
factory.h:39:13: error: invalid covariant return type for ‘std::vector<std::unique_ptr<ConcreteProduct<DATATYPE, RETURNTYPE> > > ConcreteFactory<DATATYPE, RETURNTYPE>::createProducts(DATATYPE) [with DATATYPE = int*; RETURNTYPE = int*]’
createProducts(DATATYPE _data) override;
^
factory.h:32:13: error: overriding ‘std::vector<std::unique_ptr<AbstractProduct<DATATYPE, RETURNTYPE> > > AbstractFactory<DATATYPE, RETURNTYPE>::createProducts(DATATYPE) [with DATATYPE = int*; RETURNTYPE = int*]’
createProducts(DATATYPE _data) = 0;
^
In file included from factory.cpp:1:0:
factory.h: In instantiation of ‘class ConcreteFactory<int*, int*>’:
factory.cpp:4:16: required from here
factory.h:39:13: error: invalid covariant return type for ‘std::vector<std::unique_ptr<ConcreteProduct<DATATYPE, RETURNTYPE> > > ConcreteFactory<DATATYPE, RETURNTYPE>::createProducts(DATATYPE) [with DATATYPE = int*; RETURNTYPE = int*]’
createProducts(DATATYPE _data) override;
^
factory.h:32:13: error: overriding ‘std::vector<std::unique_ptr<AbstractProduct<DATATYPE, RETURNTYPE> > > AbstractFactory<DATATYPE, RETURNTYPE>::createProducts(DATATYPE) [with DATATYPE = int*; RETURNTYPE = int*]’
createProducts(DATATYPE _data) = 0;
^
由于ConcreteProduct
是AbstractProduct
的子类,这应该有效,不应该吗?我做错了什么?