Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

list of all objects in a templated class

1 view
Skip to first unread message

Ajay 0x007

unread,
Jul 9, 2009, 5:18:23 AM7/9/09
to
Hi,
I have a templated class and I want to mantain a list pointers of all
objects of that class. How it can be achieved???

If i do like below (make list as static member variable of same class)
then the list will be different for all typename X, i.e.

template <typename X> class A{
static std::vector<class<X>* > list;
A(){
list.push_back(this);
}
};


If I make a global pointer instead of static member variable as above
then I need to provide actual argumenst to X which I cannot know in
advance.

Is their any way to achiev that?


Fred Zwarts

unread,
Jul 9, 2009, 6:44:03 AM7/9/09
to
"Ajay 0x007" <ajay....@gmail.com> wrote in message news:f3c344ba-d6a8-46c5...@r15g2000pra.googlegroups.com...

You could create a base class, which contains the list.
Then create a template class that derives from this base class.

Alan Woodland

unread,
Jul 9, 2009, 6:52:32 AM7/9/09
to

How about another class that all A's inherit from? You could store
pointers to them without any trouble... you could even add one or more
virtual methods to that class to do something useful with!

What's your motivation for doing this though? There might be other
solutions too.

Alan

Ajay 0x007

unread,
Jul 9, 2009, 7:24:45 AM7/9/09
to
> Alan- Hide quoted text -
>
> - Show quoted text -

base class mantaining the list of templated class is not possible, How
the template argument be determined in base class??? i.e.

class base {
std::vector<class A<X>* > list;
};
class A:public base{
};
How the "X" in base class will be determined?


I want to mantain a list of all pointers of a templated class which
will be extracted later on.

Alan Woodland

unread,
Jul 9, 2009, 9:03:30 AM7/9/09
to
Ajay 0x007 wrote:
> On Jul 9, 3:52 pm, Alan Woodland <aj...@aber.ac.uk> wrote:
>> Ajay 0x007 wrote:
>>> Hi,
>>> I have a templated class and I want to mantain a list pointers of all
>>> objects of that class. How it can be achieved???
>>> If i do like below (make list as static member variable of same class)
>>> then the list will be different for all typename X, i.e.
>>> template <typename X> class A{
>>> static std::vector<class<X>* > list;
>>> A(){
>>> list.push_back(this);
>>> }
>>> };
>>> If I make a global pointer instead of static member variable as above
>>> then I need to provide actual argumenst to X which I cannot know in
>>> advance.
>>> Is their any way to achiev that?
>> How about another class that all A's inherit from? You could store
>> pointers to them without any trouble... you could even add one or more
>> virtual methods to that class to do something useful with!
>>
>> What's your motivation for doing this though? There might be other
>> solutions too.
>>
>
> base class mantaining the list of templated class is not possible, How
> the template argument be determined in base class??? i.e.
>
> class base {
> std::vector<class A<X>* > list;
> };
> class A:public base{
> };
> How the "X" in base class will be determined?

base won't know the X. You could store the type_info with each instance
in the list, but that smells of bad design.

In your application is X the infinite set of all user defined types? Or
is it some small restricted subset? There are things you could
potentially do if it's a small restricted subset here too.

> I want to mantain a list of all pointers of a templated class which
> will be extracted later on.

Why do you want to examine them though? What do you hope to achieve by
examining them? Why can't this be done through a pure virtual function
in base? Something seems odd with your design here, but we don't have
enough information to go with.

Alan

Fred Zwarts

unread,
Jul 9, 2009, 10:09:11 AM7/9/09
to
>"Ajay 0x007" <ajay....@gmail.com> wrote in message news:2da11b82-0a91-4035...@f20g2000prn.googlegroups.com...

class base {
std::vector <base *> list;
base () {
list.push_back (this);
}
};

template <class x>
class A : public base {
};

This will create the list.

Using virtual functions in class base will remove the necessity the determine
the exact pointer types in the list. (Recommended method.)

If for some reason you need to access members in a derived class that are
not in base, type_info and/or dynamic_cast can be used to determine the
exact pointer type of the pointers in the list. (Not recommended.)

0 new messages