templates and polymorphism

Discussion in 'C++' started by glen stark, Mar 28, 2006.

  1. glen stark

    glen stark Guest

    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
    glen stark, Mar 28, 2006
    #1
    1. Advertising

  2. glen stark

    Markus Moll Guest

    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
    Markus Moll, Mar 28, 2006
    #2
    1. Advertising

  3. 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
    Stuart Redmann, Mar 29, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sapropel
    Replies:
    1
    Views:
    584
    Victor Bazarov
    May 14, 2004
  2. JKop
    Replies:
    3
    Views:
    451
  3. Krivenok Dmitry
    Replies:
    13
    Views:
    1,386
    Axter
    Jun 1, 2006
  4. recover
    Replies:
    2
    Views:
    784
    recover
    Jul 25, 2006
  5. sarathy

    Templates and Polymorphism

    sarathy, Jul 31, 2006, in forum: C++
    Replies:
    2
    Views:
    341
    Alan Johnson
    Jul 31, 2006
Loading...

Share This Page