J
Johan Bergman
Hi,
Maybe someone can help me with this one. The following describes a
somewhat simplified version of my problem, but I think it will be
sufficient.
I want to use class factories (virtual constructors) which have this base
class:
template<class T>
class Factory_Base {
public:
virtual ~Factory() {} // (could be pure virtual)
virtual T* create(int n) {return new T[n];} // (could be pure virtual)
};
I also have a container class, Array<T>, which has a constructor that
takes a factory as a parameter:
template<class T>
Array<T>::Array(Factory_Base<T> &f)
{
factory = &f; // Array<T> has a member Factory<T> *factory
}
This works fine as long as I only want to do this:
My_Factory my_factory; // My_Factory creates My_T instances
Array<My_T> my_variable(my_factory);
However, sometimes I want to write like this:
My_Factory my_factory; // My_Factory still creates My_T instances
Array<Array<My_T> > my_variable(my_factory);
The most straightforward solution would be to create a factory that can
create Array<My_T>, but I don't want to do that.
Please tell me that there is another way!
/ Johan
Maybe someone can help me with this one. The following describes a
somewhat simplified version of my problem, but I think it will be
sufficient.
I want to use class factories (virtual constructors) which have this base
class:
template<class T>
class Factory_Base {
public:
virtual ~Factory() {} // (could be pure virtual)
virtual T* create(int n) {return new T[n];} // (could be pure virtual)
};
I also have a container class, Array<T>, which has a constructor that
takes a factory as a parameter:
template<class T>
Array<T>::Array(Factory_Base<T> &f)
{
factory = &f; // Array<T> has a member Factory<T> *factory
}
This works fine as long as I only want to do this:
My_Factory my_factory; // My_Factory creates My_T instances
Array<My_T> my_variable(my_factory);
However, sometimes I want to write like this:
My_Factory my_factory; // My_Factory still creates My_T instances
Array<Array<My_T> > my_variable(my_factory);
The most straightforward solution would be to create a factory that can
create Array<My_T>, but I don't want to do that.
Please tell me that there is another way!
/ Johan