templates and polymorphism

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • glen stark

    #1

    templates and polymorphism

    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
  • Markus Moll

    #2
    Re: templates and polymorphism

    Hi

    glen stark wrote:
    [color=blue]
    > 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.[/color]

    Do that.
    [color=blue]
    > 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.[/color]

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

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

    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 "optimizati ons" when they shouldn't.

    Markus


    Comment

    • Stuart Redmann

      #3
      Re: templates and polymorphism

      glen stark wrote:[color=blue]
      >
      > 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?[/color]

      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.
      [color=blue]
      > This will be called many times in a loop, so if I can avoid rtti that
      > would be ideal, and worth some hassle.[/color]

      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

      Comment

      Working...