I am trying to create a template class which contains a static list of objects which there is only one of. What i have so far works but it gives me a copy of “mylist” for each different type of class B parameter. How can i change it so that i get one “mylist” for all instantiations of class B regardless of template parameters?
This is what i have:
template <class T> class A {
...
};
template <class T> class B {
static list<A<T> > mylist;
...
};
template <class T> list< A<T> > B<T>::mylist;
Thanks in advance 🙂
You can inherit from a common (non-templated) base class to ensure that there isn’t an instance for every template instantiation.
That depends on your expectations. I’m going to assume from your example that “any type” means “any instantiation of template class
A“.Since those types could vary in size, you’ll do best with holding pointers to the objects.
This is one example of a solution that would solve both those problems.