Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template and container, looking for advice

Reply
Thread Tools

template and container, looking for advice

 
 
Istvan Buki
Guest
Posts: n/a
 
      10-08-2003
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-08-2003
"Istvan Buki" <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments