templates and polymorphism

G

glen stark

I have a templated function

template<class T> void update_E(T& updateable)
{
....
}

What I would like to do is store a vector of updateable objects, and for
each of them call the update function. What and how many updateable
objects area actually created is determined at runtime.

Using polymorphism I would make an interface iUpdateable, and a bunch of
subclasses, and store a vector of iUpdateable pointers.

Somehow this seems klunky to me, and I'm wondering if there's a
cleverer way of doing this. I don't need polymorphis or inheritance
except to allow storage of multiple types. I've heard of something
called "static polymorphism". Would that apply here?

This will be called many times in a loop, so if I can avoid rtti that
would be ideal, and worth some hassle.



Thanks,

Glen
 
M

Markus Moll

Hi

glen said:
What I would like to do is store a vector of updateable objects, and for
each of them call the update function. What and how many updateable
objects area actually created is determined at runtime.

Using polymorphism I would make an interface iUpdateable, and a bunch of
subclasses, and store a vector of iUpdateable pointers.

Do that.
Somehow this seems klunky to me, and I'm wondering if there's a
cleverer way of doing this. I don't need polymorphis or inheritance
except to allow storage of multiple types.

You need it because the type of your objects is only determined at runtime.
I've heard of something called "static polymorphism". Would that apply
here?

No. The word "static" immediately contradicts your requirement that the
function to be applied can only be determined at runtime.
This will be called many times in a loop, so if I can avoid rtti that
would be ideal, and worth some hassle.

Not if you don't know the type of your objects in advance.
How many times are we talking about anyway?
Often people worry too much about "optimizations" when they shouldn't.

Markus
 
S

Stuart Redmann

glen said:
I have a templated function

template<class T> void update_E(T& updateable)
{
...
}

What I would like to do is store a vector of updateable objects, and for
each of them call the update function. What and how many updateable
objects area actually created is determined at runtime.

Using polymorphism I would make an interface iUpdateable, and a bunch of
subclasses, and store a vector of iUpdateable pointers.

Somehow this seems klunky to me, and I'm wondering if there's a
cleverer way of doing this. I don't need polymorphis or inheritance
except to allow storage of multiple types. I've heard of something
called "static polymorphism". Would that apply here?

Nope. Static polymorphism is used to describe the overloading feature of
C++. If you think about this it is not too bad a name for it. If you
have some method func (some parameter), it can behave differently
depending on which type of parameters I pass (overloading == static
polymorphism) or depending on from which subclass I call func (==
dynamic(?) polymorphism).

What you need in your case is plain old 'dynamic' (I'm not sure if it is
called this way) polymorphism. You have a collection of objects, and for
each object the behaviour of the update method is different. Your
solution is far from being clunky.
This will be called many times in a loop, so if I can avoid rtti that
would be ideal, and worth some hassle.

No rtti is needed in this case. Which version of your update method is
called is decided by the VMT (virtual method table) that is created for
each sub-class you define. RTTI is only needed to be able to do dynamic
upcasts.

Regards,
Stuart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top