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