template and container, looking for advice

Discussion in 'C++' started by Istvan Buki, Oct 8, 2003.

  1. Istvan Buki

    Istvan Buki Guest

    Dear C++ gurus,

    I'm having some problems inserting some template classes into containers.
    I'm looking for ideas, suggestions,... on how to achieve this but first let
    me explain my problem. Everything starts with a simple template class like
    the one below. In this example class A has two template parameters but that
    number can be different.

    template < typename T, typename U >
    class A
    void a_method( T *t ) ;

    // Other methods...

    // private stuff...
    } ;

    Now let's say that in the application using class A, I instantiate A with 10
    different values for the T template parameter and I create one object of
    each type.

    To keep track of all these objects I would like to put them in a stl
    container but I can't because all the objects have a different type.

    I thought about creating a base class and make A derive from it but it
    doesn't work because of the "a_method" that depends on one of the template
    parameter and I cannot create a pure virtual a_method in the base class.

    Then I thought about changing the signature of the "a_method" to something
    like this:

    void a_method( BaseT *t );

    and make sure that template parameter T is derived from BaseT but it is not
    what I need because if I reuse class A in another application I would be
    forced to derive all classes used as T template parameter from BaseT which
    is probably not meaninful in the context of the new application.

    Finally I thought about doing the following:

    template < typename BaseT >
    class ParentA
    typedef BaseT BaseType ;

    virtual void a_method( BaseT *t ) = 0 ;

    // Other pure virtual methods...
    } ;

    template < typename MyParent, typename T, typename U >
    class A : public MyParent
    void a_method( MyParent::BaseType *t ) ;

    // ...
    } ;

    Then using it like this for example:

    struct SomeBaseClass
    // ...
    } ;

    struct SomeDerivedClass : public SomeBaseClass
    // ...
    } ;

    typedef ParentA< SomeBaseClass > ParentExample ;
    typedef A< ParentExample, SomeDerivedClass, SomeUType > ;

    This works but it is not very nice and it forces the user of the class to
    code a lot of stuff he should not worry about.
    And here I'm stuck. Does anybody have some ideas about how to achieve the
    same goal but in cleaner (possibly simpler) way ?

    Thank you for your help,
    Istvan Buki, Oct 8, 2003
    1. Advertisements

  2. "Istvan Buki" <> wrote...
    > I'm having some problems inserting some template classes into containers.
    > [...]
    > And here I'm stuck. Does anybody have some ideas about how to achieve the
    > same goal but in cleaner (possibly simpler) way ?

    What makes you think that you need all those [apparently unrelated]
    types in one and the same container? And if they are related [by
    the fact that they are in the same container], how are you using
    that fact? The necessity to call particular [and not polymorphic]
    member functions is not good enough reason to put them in one place.

    The solution you presented is about the only way you can achieve
    what you [seem to] need. It's clean and simple. The classes are
    made related by the common base class, the template arguments are
    also forced to be related. Without that you cannot use polymorphism
    [which you apparently want to use, although unclear as to _why_].


    P.S. It is usually advised to present the _problem_ you're trying
    to solve instead of a non-working "solution" to that problem. Without
    seeing what you're trying to achieve it is rather difficult to suggest
    a decent solution.
    Victor Bazarov, Oct 8, 2003
    1. Advertisements

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. Vivi Orunitia
    Martijn Lievaart
    Feb 4, 2004
  2. Maitre Bart
    Maitre Bart
    Feb 11, 2004
  3. Steven T. Hatton
    Rob Williscroft
    Dec 5, 2004
  4. Replies:
    Daniel T.
    Feb 16, 2006
  5. er
    Sep 5, 2007

Share This Page