我试图有一个通用的基类/帮助器类,它为调用类分配shared_ptrs,但是我在使它在派生类中工作时遇到了问题。
#include <memory>
template<typename T>
struct SPAlloc {
virtual ~SPAlloc() {}
template<typename ...Args>
static std::shared_ptr<T>
Alloc(Args&&... params) {
return std::make_shared<T>(std::forward<Args>(params)...);
}
template<class U, typename ...Args>
static std::shared_ptr<U>
Alloc(Args&&... params) {
return std::make_shared<U>(std::forward<Args>(params)...);
}
};
class Base : public SPAlloc<Base> {
public:
virtual ~Base() {};
};
class Child : public Base {
public:
virtual ~Child() {};
};
typedef std::shared_ptr<Base> pBase;
typedef std::shared_ptr<Child> pChild;
int main() {
pBase base = Base::Alloc();
pChild child = Child::Alloc();
}
我知道
class Base : public SPAlloc<Base>
表示模板中的T
将是Base
,这就是我创建第二个Alloc的原因。第二个alloc需要像Child::Alloc<Child>()
那样被调用。有没有办法编写此
Alloc
,以便编译器可以推断出我正在调用Alloc的类? 最佳答案
简短的回答:不,没有。
长答案:关键是Alloc
除非明确告知,否则不知道Child
,那么这些信息将从何而来?对Child::Alloc()
的调用是对Base::Alloc()
的调用,这是对SPAlloc<Base>::Alloc()
的调用,因此,有关Child
的所有信息都将丢失。
最简单的解决方案是使用自由函数,但是该函数已经存在,并且称为:std::make_shared
。也许考虑直接使用它并完全避免SPAlloc
的麻烦。
另外,如果要为每个孩子覆盖SPAlloc<T>::Alloc()
,则根本不需要基类SPAlloc
,只需将方法添加到每个类中,这可能比使用基类容易。
关于c++ - 在基类中只有一个方法可以分配子类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15605223/