template and container, looking for advice
From: Istvan Buki (istvan.buki_at_euronet.be)
Date: 10/08/03
- Next message: tom_usenet: "Re: convert data type help"
- Previous message: tom_usenet: "Re: convert data type help"
- Next in thread: Victor Bazarov: "Re: template and container, looking for advice"
- Reply: Victor Bazarov: "Re: template and container, looking for advice"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 08 Oct 2003 13:55:39 +0200
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
{
public:
void a_method( T *t ) ;
// Other methods...
private:
// 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
{
public:
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
{
public:
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
- Next message: tom_usenet: "Re: convert data type help"
- Previous message: tom_usenet: "Re: convert data type help"
- Next in thread: Victor Bazarov: "Re: template and container, looking for advice"
- Reply: Victor Bazarov: "Re: template and container, looking for advice"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|