Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > templates and polymorphism

Reply
Thread Tools

templates and polymorphism

 
 
glen stark
Guest
Posts: n/a
 
      03-28-2006
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
 
Reply With Quote
 
 
 
 
Markus Moll
Guest
Posts: n/a
 
      03-28-2006
Hi

glen stark wrote:

> 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


 
Reply With Quote
 
 
 
 
Stuart Redmann
Guest
Posts: n/a
 
      03-29-2006
glen stark wrote:
>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Templates and Polymorphism sarathy C++ 2 07-31-2006 09:09 AM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Dynamic polymorphism vs. Static polymorphism Krivenok Dmitry C++ 13 06-01-2006 09:49 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
trying to understand Inheritance, Polymorphism and templates sapropel C++ 1 05-14-2004 02:25 PM



Advertisments