list of all objects in a templated class

A

Ajay 0x007

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?
 
F

Fred Zwarts

Ajay 0x007 said:
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?

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

Alan Woodland

Ajay said:
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.

Alan
 
A

Ajay 0x007

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- 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.
 
A

Alan Woodland

Ajay said:
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
 
F

Fred Zwarts

Ajay 0x007 said:
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?

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.)
 

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,787
Messages
2,569,627
Members
45,328
Latest member
66Teonna9

Latest Threads

Top