很长一段时间的读者,我想一个模板,返回不同的类
我在想,如果有人可以帮助我做我的代码简单一点使用模板返回从相同的基本派生不同的类。所以我可以调用类的函数而不是重写管理器类中的每个对象调用的函数。这里一个我目前的函数的例子电话:
void setLoopImage(AnimationManagerIndex & managerIndex, bool b)
{
switch (managerIndex.animationClass)
{
case ANIMATION_CLASS_BASE:
{
vAnimationObjectBase[managerIndex.index].setLoopImage(b);
break;
}
case ANIMATION_CLASS_MOVING:
{
vAnimationObjectMoving[managerIndex.index].setLoopImage(b);
break;
}
case ANIMATION_CLASS_MOVING_BEZIER:
{
vAnimationObjectMovingBezier[managerIndex.index].setLoopImage(b);
break;
}
}
}
我想创建一个返回像这样这些类模板函数:
template<class T>
T & getAnimationObject(AnimationManagerIndex & managerIndex)
{
switch (managerIndex.animationClass)
{
case ANIMATION_CLASS_BASE:
{
return vAnimationObjectBase[managerIndex.index];
}
case ANIMATION_CLASS_MOVING:
{
return vAnimationObjectMoving[managerIndex.index];
}
case ANIMATION_CLASS_MOVING_BEZIER:
{
return vAnimationObjectMovingBezier[managerIndex.index];
}
}
}
所以第一个函数将变为:
void setLoopImage(AnimationManagerIndex & managerIndex, bool b)
{
getAnimationObject(managerIndex).setLoopImage(b);
}
你说这三个是从相同的基本衍生?然后你不需要模板。只需使用'Base&'作为返回类型即可。 – HolyBlackCat
你的尝试有什么问题?你有什么实际的*问题?作为一个长期的读者,你应该有时间[阅读如何提出好问题](http://stackoverflow.com/help/how-to-ask),如果你还没有这样做,那么请这样做。 –