The problem seems to be that if i create a list of Tweens, then call update, it uses Tweens implementation of Tween.update() and not it's inherited children, I don't know how/if it's possible to allow this...
Of course it's possible, that's the whole idea of polymorphism. You just need to ensure two things:
Mark the
Updatemethod as virtual in yourTweenbase class:class Tween { public: virtual void Update(float elapsed) {} virtual ~Tween() {} }
Note #1: It's really important that you make your destructor virtual too whenever you intend to use polymorphism. This is needed so that you can delete the objects correctly through a pointer to the base class.
Make sure that you create your list so that it holds pointers to
Tweeninstead ofTweenobjects directly:std::vector<Tween*> tweens;
Afterwards all you have to do is the fill the list with objects of any type that inherits from Tween and when calling Update on them the program will automatically choose the right version for each object. No extra code needed:
tweens.push_back(new Anim(parameters));
tweens.push_back(new Move(parameters));
tweens.push_back(new Rotate(parameters));
for(int i=0; i!=tweens.size(); ++i)
{
// Will call the correct version of Update for each object in the list
// Because Update is marked as virtual and tweens[i] is a pointer
tweens[i]->Update();
}
Note #2: And since it doesn't make sense to create a
Tweenobject directly, you should also make the class abstract to prevent this behavior explicitally. In C++ this is done by adding one or more pure virtual methods to the class. When all methods are pure virtual and there are no member variables in the class, it's also known as an interface. The above example would become:
class ITweenTween
{
public:
virtual void Update(float elapsed) = 0;
virtual ~Tween() = 0;{}
}