派生类和模板参数
Derrived Classes and Template Arguments
对不起,标题含糊不清。
我试图在一个相当大的代码库中解决一个问题。该修复程序要求我将ChildClass
和ChildListElementClass
都作为模板参数与另一个一起提供。我相信我遇到的问题是ParentListElementClass
中的myType
没有被派生类ChildListElementClass
中的typedef ChildClass<ChildListElementClass> myType;
覆盖。我怎样才能做到这一点?谢谢!
编译器 说:
Error 1 error C2664: 'void ParentListElementClass<ChildClass>::AddX(ParentClass<ParentListElementClass<ChildClass>> *)' : cannot convert argument 1 from 'ParentClass<ChildListElementClass> *const ' to 'ParentClass<ParentListElementClass<ChildClass>> *'
我写了以下最小的测试用例来演示我的问题:
#include "stdafx.h"
#include <iostream>
template <class ElementType>
class ParentClass;
template <class ParentType>
class ParentListElementClass
{
public:
ParentListElementClass(){};
~ParentListElementClass(){};
typedef ParentClass<ParentListElementClass> myType;
myType *x;
void AddX(myType *m){
m->speak();
};
};
class ChildListElementClass;
template <class ElementType>
class ParentClass
{
public:
ParentClass(){};
~ParentClass(){};
ElementType m;
void DoSomething() {
std::cout << "ParentListn";
m.AddX(this);
}
virtual void speak(){ std::cout << "Parentn";}
};
class ChildClass;
class ChildListElementClass : public ParentListElementClass<ChildClass>
{
public:
ChildListElementClass(){};
~ChildListElementClass(){};
};
class ChildClass : public ParentClass<ChildListElementClass>
{
public:
ChildClass(){};
~ChildClass(){};
void speak(){ std::cout << "Childn"; }
};
int _tmain(int argc, _TCHAR* argv[])
{
ChildClass Child;
ChildListElementClass ChildListElement;
Child.DoSomething();
return 0;
}
编辑:在鼬的帮助下工作版本:
template <typename ElementType, typename ListType> class ParentListClass;
template <typename ElementType, typename ListType>
class ParentElementClass
{
typedef ParentListClass<ElementType, ListType> myType;
};
template <typename ElementType, typename ListType>
class ParentListClass{};
class ChildListClass;
class ChildElementClass : public ParentElementClass<ChildElementClass, ChildListClass>{};
class ChildListClass : public ParentListClass<ChildElementClass, ChildListClass>{};
我不确定我是否完全理解你的问题,但无论我怎么想,更改 ParentListElementClass 似乎都很难避免。你可以在这些方面尝试一些东西。
template<typename T, typename U>
struct MyTypedefCase
{
typedef T<U> _type;
};
然后将 ParentListElementClass 重写为
template <class ParentType, typename T, typename U>
class ParentListElementClass
{
//typedef ParentClass<ParentListElementClass> myType; Change this to below line
typedef typename MyTypedefCase<T,U>::_type myType;
//Rest of your code
};
和
class ChildListElementClass : public ParentListElementClass<ChildClass,ChildClass,ChildListElementClass>
我从来没有用模板做过这么复杂的事情,所以我不保证它会起作用,但如果我必须以最小的更改去做,那就是这样。也许有人可以研究这个解决方案并提供更改或改进。
相关文章:
- 在派生函数中指定void*参数
- C++重载函数,一个采用基类的参数,另一个采用派生类的参数
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 如何在不使用指针的情况下将派生类的对象作为参数传递给基类中的函数?
- 创建派生自可变参数模板包的类型元组
- 如何将成员函数作为参数传递并在派生对象上执行方法列表
- 派生类(构造函数具有参数)和基类(构造函数缺少参数)之间没有可行的转换
- C++派生类重载函数(带有 std::function 参数)不可见
- C++ 事件管理器的回调,使用 std::function 和 std:bind 以及派生类作为参数
- 从模板参数派生的类
- C++ 如何使派生类自动获取基类参数
- C++ 将派生类的成员函数指针作为参数传递时选择了错误的模板专用化
- 如何在派生类中正确覆盖具有不同参数的函数?
- 根据参数创建派生类的新实例
- 具有派生参数的函数重写
- 将派生类作为参数传递给方法,该方法是具有智能指针的基类
- 将方法参数类型更改为子类中的派生类
- 派生类的模板参数推导
- 将派生类构造函数参数传递给受保护的成员
- 具有原始方法参数派生类的 C++ 重载方法参数