473,513 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector of pointers to instances of a templated class

Hi all,

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

template <typename T, typename uclass 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
Aug 8 '06 #1
5 5463
In article <eb**********@ikaria.belnet.be>, gv******@gmail.com says...
Hi all,

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

template <typename T, typename uclass 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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 8 '06 #2
Jerry Coffin wrote:
In article <eb**********@ikaria.belnet.be>, gv******@gmail.com says...
>Hi all,

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

template <typename T, typename uclass 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.
yes, I wasn't clear enough.

I have three objects:

A<int,intobj1;
A(double,intobj2;
A(double,doubleobj3;

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
>
>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.
Aug 8 '06 #3
Gert Van den Eynde wrote:
Hi all,

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

template <typename T, typename uclass 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

Aug 8 '06 #4
Gert Van den Eynde wrote:
>
I have three objects:

A<int,intobj1;
A(double,intobj2;
A(double,doubleobj3;

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.

--
Ian Collins.
Aug 8 '06 #5

Gert Van den Eynde wrote:
Jerry Coffin wrote:
In article <eb**********@ikaria.belnet.be>, gv******@gmail.com says...
Hi all,

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

template <typename T, typename uclass 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.
yes, I wasn't clear enough.

I have three objects:

A<int,intobj1;
A(double,intobj2;
A(double,doubleobj3;

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,inthas 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 T2class A: public A_Base
{
......
};

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

/Peter
thanks,
gert
[snip]

Aug 8 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2956
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
14
3508
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers...
34
4128
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
8
5011
by: He Shiming | last post by:
Hi, I've developed a class that implements an interface definition. It looks like this: class IRecord { public: // define interface methods by pure virtual methods // no member variables }
8
18503
by: Manuel | last post by:
Hi! If I've a vector filled with abstract classes, can I push in it the derived classes too? Even if derived classes have new methods? I've done some experiments, and it seem I can push the...
22
3147
by: sandy | last post by:
I am trying to make a simulated directory structure application for a course. I am using a Vector to store pointers to my Directory objects (as subdirectories of the current object). In my...
4
2469
by: majsta | last post by:
Hello, I have the following code. #include <vector> #include <iostream> class Foo { public: Foo(){} virtual void print() const { std::cout << "foo" << std::endl;} };
3
1572
by: Rob | last post by:
I have these classes (elided methods): class Base { public: Base(string name) {...} }; class Derived : public Base {
7
2666
by: Ralf Goertz | last post by:
Hi, the following templated code doesn't compile. It gives the error: template_it.cc:17: error: type 'std::vector<Derived1<T>*, std::allocator<Derived1<T>*' is not derived from type...
1
7111
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7539
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5692
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5095
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4751
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3228
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
461
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.