模板类型依赖和继承
template type dependecy and inheritance
我的模板方法定义为
template<class T>
const std::list<T>& GetList()
{
return GetList(std::shared_ptr<Parent>(new T()));
}
const std::list<std::shared_ptr<Parent>>& GetList(const std::shared_ptr<Parent>&a)
{
//
}
鉴于T
是将父母作为父班的类的类型,例如
class Child1:public Parent
{};
编译错误: Can not convert const std::list<std::shared_ptr<Parent>>& to const std::list<T>&
有什么方法可以解决此问题,而无需将T
的类型更改为共享_ptr?
定义必须匹配。
typedef std::shared_ptr<Parent> ParentPtr;
std::list<ParentPtr> GetParentList(const ParentPtr& a)
{
//
}
template<class Child>
std::list<ParentPtr> GetParentListT()
{
ParentPtr parent_ptr(new Child);
return GetList(parent_ptr);
}
...
std::list<ParentPtr> my_list = GetParentListT<Child1>();
还确保在使用此代码之前定义了Child1
(不仅是声明),否则编译器不会知道它是Parent
的子类。
相关文章:
- 初始化依赖于子类的继承类的常量类成员
- 循环依赖,在继承类的情况下使用覆盖方法
- 当依赖关系和依赖关系都是多态时,在哪个继承级别存储依赖关系指针?
- 正在从继承中解析循环依赖项
- 继承依赖类型定义而不使用结构
- 相互依赖的类和继承
- C++ 上的循环依赖和继承编译错误
- 模板类型依赖和继承
- 依赖注入/继承设计模式的构造函数参数太多
- 如何在Visual Studio中将项目依赖项/引用从一个项目继承到依赖项目
- C++中对继承的循环依赖
- 依赖类的C++继承和实例化
- 在多继承编译器的情况下访问成员变量是否依赖?如何正确地做
- 如何在C++中管理并行的依赖继承
- C++相互依赖的类的继承
- C++循环依赖和继承
- 处理对继承的循环依赖
- 模板继承:没有依赖模板形参的实参
- 具有继承模板类的循环依赖关系
- 如何使继承依赖