Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   template and container, looking for advice (http://www.velocityreviews.com/forums/t278240-template-and-container-looking-for-advice.html)

Istvan Buki 10-08-2003 11:55 AM

template and container, looking for advice
 
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


Victor Bazarov 10-08-2003 03:55 PM

Re: template and container, looking for advice
 
"Istvan Buki" <istvan.buki@euronet.be> 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_].

Victor

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.




All times are GMT. The time now is 09:14 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57