vector of pointers to instances of a templated class

  • Thread starter Gert Van den Eynde
  • Start date
G

Gert Van den Eynde

Hi all,

It's probably trivial but I can't figure it out... I have a templated class

template <typename T, typename u> class A

I wish to fill a vector with pointers to objects of class A. I tried to
declare the vector as

std::vector< A<>* > vA

but this doesn't work....

Can anyone explain how and why?

thanks,
gert
 
J

Jerry Coffin

Hi all,

It's probably trivial but I can't figure it out... I have a templated class

template <typename T, typename u> class A

I wish to fill a vector with pointers to objects of class A.

A is not a class -- it's a template for a nearly infinite variety of
classes.
I tried to declare the vector as

std::vector< A<>* > vA

As you've found, this doesn't work. You're probably going to have to re-
think your strategy a bit here. Creating a vector of pointers isn't too
bad, but as a general rule, you want to know what they're pointing at to
be able to do much with them. If you really just want pointers, and
don't care what type they're pointing at, you can use a pointer to void.

If you really want all the pointers in one collection to point at the
same type of object, you're probably going to have to templatize your
container code as well:

template <class t, class u>
whatever {
std::vector<A<t, u> > vA;

// ...
};

Then when you instantiate whatever over some pair of types, you also
create vA as a container of A instantiated over those same types. You
can't directly form a vector of (pointers to) A instantiated over all
possible types though.
 
G

Gert Van den Eynde

Jerry said:
A is not a class -- it's a template for a nearly infinite variety of
classes.
yes, I wasn't clear enough.

I have three objects:

A<int,int> obj1;
A(double,int> obj2;
A(double,double> obj3;

I wish to put all three of them in the same vector. Reason is that I
wish to loop over the vector and perform an action on each object,
regardless of what the template parameters were (the objects have the
same interface). So I don't want to instantiate the vector with a
certain set of template parameters.... Is the void* then the only option?

thanks,
gert
 
K

Kai-Uwe Bux

Gert said:
Hi all,

It's probably trivial but I can't figure it out... I have a templated
class

template <typename T, typename u> class A

I wish to fill a vector with pointers to objects of class A.

A is not a class. It is a template. It allows you to generate a multitude of
classes by specifying the template parameters.
I tried to declare the vector as

std::vector< A<>* > vA

but this doesn't work....

No it does not and it is not supposed to.

Can anyone explain how and why?

Templates are not classes.


More important: what is the underlying problem that you are trying to solve?


Best

Kai-Uwe Bux
 
I

Ian Collins

Gert said:
I have three objects:

A<int,int> obj1;
A(double,int> obj2;
A(double,double> obj3;

I wish to put all three of them in the same vector. Reason is that I
wish to loop over the vector and perform an action on each object,
regardless of what the template parameters were (the objects have the
same interface). So I don't want to instantiate the vector with a
certain set of template parameters.... Is the void* then the only option?

thanks,
gert
Create a (possibly abstract) base class for the template A and use a
vector of pointers to the base class.
 
P

peter koch

Gert said:
yes, I wasn't clear enough.

I have three objects:

A<int,int> obj1;
A(double,int> obj2;
A(double,double> obj3;

I wish to put all three of them in the same vector. Reason is that I
wish to loop over the vector and perform an action on each object,
regardless of what the template parameters were (the objects have the
same interface). So I don't want to instantiate the vector with a
certain set of template parameters.... Is the void* then the only option?

First of, don't toppost. This makes reading your post (and following
posts such as this one) more difficult. And now to the real stuff!

The problem with your approach is that template-instantations are
unrelated. So your A<int,int> has no more relations to A<double,int>
than it has with e.g. std::string. Thus what you are in effect saying
is that you want an array where the first element points to an integer,
the second to a double and the third element points to a std::string.
As you know, this is just not possible.
Thus, the correct way is to either use a struct with three elements or
force your A-classes to share some functionality - something like this
untested sketch:

class A_base
{
virtual void action() = 0;
virtual ~A_Base() {}
};

template<typename T1,typename T2> class A: public A_Base
{
......
};

.....
std::vector<A_Base*> v:

/Peter
thanks,
gert
[snip]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top