Class templates and singleton container
I want to write a (singleton) container for instances of my class
templates, however, I am not too sure on how to:
1). Store the instances
2). How to write the acccesor method (instance()) to retrieve an
instance of particular template
3). What type to return an instance as ..
Assuming I have the following code:
class template
template <class T1, class T2>
class MyTree
{
T1 foo(const T2& a1);
T2 foobar(const T1& a1, const T2& a2);
};
// Notes
// Instance prototype not completed (see question 2 and 3)
// Ideally, when an instance of a particular class is requested, if it
dosen't yet exist, then an
// instance is created and stored in the 'repository'
class Container
{
instance();
};
//Main.cpp
int main(int argc, char* argv[])
{
Container c;
MyTree<double, int> * t1 = c.instance(/*some args here*/);
MyTree<string, double> *t2 = c.instance(/*some args here*/);
}
Last but not the least, I want to be able to treat objects returned by
the instance() method, in a generic way (i.e. in this example, I want
to be able to treat them generically, as trees). Should I use
inheritance (i.e. the class template inherits from a base Tree class)
like this:
template <class T1, class T2>
class MyTree : public Tree
{
T1 foo(const T2& a1);
T2 foobar(const T1& a1, const T2& a2);
};
or is there a better way?