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 6 2002
Hi,
I made a few typos in my first post so here it all comes again (corrected).
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_Base() {} // (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_Base<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
"Johan Bergman" <us**@inter.net> wrote... 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_Base() {} // (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_Base<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!
You haven't shown how 'Array<>' uses the factory. For all we know,
the way it uses it should allow for "de-Array-fication". You could
write some kind of recursive template to "de-Array-fy" the template
argument until it reaches non-Array-derived type.
template<class T> class Array;
template<class T> class NonArray {
typedef T original;
};
// specialisation for 'Array<T>'
template<class T> class NonArray<Array<T> > {
typedef NonArray<T>::original original; // recursive
};
template<class T> class Array {
typedef Factory_Base< NonArray<T>::original > Real_Factory;
// use Real_Factory somehow
};
This is just a quick sketch, a nugde so to speak. Perhaps you can
dig something useful from it...
Victor
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:ywhNb.66727$xy6.124703@attbi_s02... "Johan Bergman" <us**@inter.net> wrote... 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_Base() {} // (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_Base<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! You haven't shown how 'Array<>' uses the factory.
Oops, you're right! My intention was to let Array<> call factory->create()
whenever it is supposed to create new (non-container) elements, i.e. when
the Array<> is initialized or resized.
For all we know, the way it uses it should allow for "de-Array-fication". You could write some kind of recursive template to "de-Array-fy" the template argument until it reaches non-Array-derived type.
template<class T> class Array;
template<class T> class NonArray { typedef T original; };
// specialisation for 'Array<T>' template<class T> class NonArray<Array<T> > { typedef NonArray<T>::original original; // recursive };
template<class T> class Array { typedef Factory_Base< NonArray<T>::original > Real_Factory; // use Real_Factory somehow };
This is just a quick sketch, a nudge so to speak. Perhaps you can dig something useful from it...
Yes, I think this helps a lot, thanks! (Perhaps you can recommend some good
books where I can learn more about this kind of C++ idioms?)
But now I have another problem. The Array<T> method that creates new
(container or non-container) elements should look something like this:
if (factory == 0)
// No factory was specified by the user
data = new T[n]; // line 3
else if (typeid(T) == typeid(Original))
// Time to use Real_Factory
data = factory->instantiate(n);
else
// T must be an Array of something
data = new T[n](*factory); // line 9
The library I am working on contains some Array<T> instantiations for types
T that don't have constructors that take a factory as a parameter. This
means that the compiler won't accept line 9 (although this line will never
be executed for such types because they will in practice be taken care of by
line 3). Is there any legal way to do this?
/ Johan
I made a typo again. In the post I just made I wrote create() in one place
an instantiate() in another place, but I meant the same function. Below you
will find a corrected version of the post.
/ Johan
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:ywhNb.66727$xy6.124703@attbi_s02... "Johan Bergman" <us**@inter.net> wrote... 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_Base() {} // (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_Base<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! You haven't shown how 'Array<>' uses the factory.
Oops, you're right! My intention was to let Array<> call factory->create()
whenever it is supposed to create new (non-container) elements, i.e. when
the Array<> is initialized or resized.
For all we know, the way it uses it should allow for "de-Array-fication". You could write some kind of recursive template to "de-Array-fy" the template argument until it reaches non-Array-derived type.
template<class T> class Array;
template<class T> class NonArray { typedef T original; };
// specialisation for 'Array<T>' template<class T> class NonArray<Array<T> > { typedef NonArray<T>::original original; // recursive };
template<class T> class Array { typedef Factory_Base< NonArray<T>::original > Real_Factory; // use Real_Factory somehow };
This is just a quick sketch, a nudge so to speak. Perhaps you can dig something useful from it...
Yes, I think this helps a lot, thanks! (Perhaps you can recommend some good
books where I can learn more about this kind of C++ idioms?)
But now I have another problem. The Array<T> method that creates new
(container or non-container) elements should look something like this:
if (factory == 0)
// No factory was specified by the user
data = new T[n]; // line 3
else if (typeid(T) == typeid(Original))
// Time to use Real_Factory
data = factory->create(n);
else
// T must be an Array of something
data = new T[n](*factory); // line 9
The library I am working on contains some Array<T> instantiations for types
T that don't have constructors that take a factory as a parameter. This
means that the compiler won't accept line 9 (although this line will never
be executed for such types because they will in practice be taken care of by
line 3). Is there any legal way to do this?
/ Johan
"Johan Bergman" <us**@inter.net> wrote... I made a typo again. In the post I just made I wrote create() in one place an instantiate() in another place, but I meant the same function. Below
you will find a corrected version of the post.
/ Johan
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:ywhNb.66727$xy6.124703@attbi_s02... "Johan Bergman" <us**@inter.net> wrote... 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_Base() {} // (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_Base<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! You haven't shown how 'Array<>' uses the factory.
Oops, you're right! My intention was to let Array<> call factory->create() whenever it is supposed to create new (non-container) elements, i.e. when the Array<> is initialized or resized.
For all we know, the way it uses it should allow for "de-Array-fication". You could write some kind of recursive template to "de-Array-fy" the template argument until it reaches non-Array-derived type.
template<class T> class Array;
template<class T> class NonArray { typedef T original; };
// specialisation for 'Array<T>' template<class T> class NonArray<Array<T> > { typedef NonArray<T>::original original; // recursive };
template<class T> class Array { typedef Factory_Base< NonArray<T>::original > Real_Factory; // use Real_Factory somehow };
This is just a quick sketch, a nudge so to speak. Perhaps you can dig something useful from it...
Yes, I think this helps a lot, thanks! (Perhaps you can recommend some
good books where I can learn more about this kind of C++ idioms?)
I recommend both "Modern C++ Design" by Alexandrescu and "C++ Templates"
by Vandevoorde and Josuttis. Very good books. Try the reverse order,
though. MC++D can be a bit too much.
Also, see Boost library for tons of template tricks. And monitor this
newsgroup. I've seen many interesting posts on templates.
But now I have another problem. The Array<T> method that creates new (container or non-container) elements should look something like this:
if (factory == 0) // No factory was specified by the user data = new T[n]; // line 3 else if (typeid(T) == typeid(Original)) // Time to use Real_Factory data = factory->create(n); else // T must be an Array of something data = new T[n](*factory); // line 9
The library I am working on contains some Array<T> instantiations for
types T that don't have constructors that take a factory as a parameter. This means that the compiler won't accept line 9 (although this line will never be executed for such types because they will in practice be taken care of
by line 3). Is there any legal way to do this?
I don't know. My ability to write template code is still far from
advanced. I would probably try to specialise the 'create' function
based on 'T', which suggest that it should be a member template...
Victor
Thanks, your answers helped me a lot!
BR,
Johan
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:3PFNb.77410$na.42900@attbi_s04... "Johan Bergman" <us**@inter.net> wrote... I made a typo again. In the post I just made I wrote create() in one
place an instantiate() in another place, but I meant the same function. Below you will find a corrected version of the post.
/ Johan
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:ywhNb.66727$xy6.124703@attbi_s02... "Johan Bergman" <us**@inter.net> wrote... > 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_Base() {} // (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_Base<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!
You haven't shown how 'Array<>' uses the factory.
Oops, you're right! My intention was to let Array<> call
factory->create() whenever it is supposed to create new (non-container) elements, i.e.
when the Array<> is initialized or resized.
For all we know, the way it uses it should allow for "de-Array-fication". You could write some kind of recursive template to "de-Array-fy" the template argument until it reaches non-Array-derived type.
template<class T> class Array;
template<class T> class NonArray { typedef T original; };
// specialisation for 'Array<T>' template<class T> class NonArray<Array<T> > { typedef NonArray<T>::original original; // recursive };
template<class T> class Array { typedef Factory_Base< NonArray<T>::original > Real_Factory; // use Real_Factory somehow };
This is just a quick sketch, a nudge so to speak. Perhaps you can dig something useful from it...
Yes, I think this helps a lot, thanks! (Perhaps you can recommend some
good books where I can learn more about this kind of C++ idioms?)
I recommend both "Modern C++ Design" by Alexandrescu and "C++ Templates" by Vandevoorde and Josuttis. Very good books. Try the reverse order, though. MC++D can be a bit too much.
Also, see Boost library for tons of template tricks. And monitor this newsgroup. I've seen many interesting posts on templates.
But now I have another problem. The Array<T> method that creates new (container or non-container) elements should look something like this:
if (factory == 0) // No factory was specified by the user data = new T[n]; // line 3 else if (typeid(T) == typeid(Original)) // Time to use Real_Factory data = factory->create(n); else // T must be an Array of something data = new T[n](*factory); // line 9
The library I am working on contains some Array<T> instantiations for types T that don't have constructors that take a factory as a parameter. This means that the compiler won't accept line 9 (although this line will
never be executed for such types because they will in practice be taken care
of by line 3). Is there any legal way to do this?
I don't know. My ability to write template code is still far from advanced. I would probably try to specialise the 'create' function based on 'T', which suggest that it should be a member template...
Victor This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: MPowell |
last post by:
I'm going through the Koeing book Accelerated C++ in an attempt to
understand Container classes. Of course I'm going through a paradigm
shift from C to C++.
So now I've got
struct Header
{...
|
by: Ronald Landheer-Cieslak |
last post by:
Hello all,
We're coding a really nice implementation of a generic abstract factory
at the moment, but we've come to a slight impass we have to work around:
when automagically registering a class...
|
by: Jon Slaughter |
last post by:
I've managed to put together a template class that basicaly creates a
recursive tree that lets you easily specify the "base" class of that tree
and and ending notes and lets you stop the recursive...
|
by: PengYu.UT |
last post by:
Hi,
Maybe this is an simple question. How can I insure that the objects of
a class must be allocated in the heap?
Thanks,
Peng
|
by: lou zion |
last post by:
hey all,
i've got a class that A that has a static class member
static int MajorMode;
it's important that if one instance of the class changes this variable, all
instances react...
|
by: Manuel |
last post by:
Hi,
I need implement a map of member functions of some class.
This map is formed by a string and a pointer to the member function.
The problem is that the map need that the object saved are...
|
by: rep_movsd |
last post by:
Hi folks
I was on topcoder and came across an interesting problem...
It involved dynamic programming ( storing function results in a map to
avoid repeated computation ) and I ended up having...
|
by: Juha Nieminen |
last post by:
I'm sure this is not a new idea, but I have never heard about it
before. I'm wondering if this could work:
Assume that you have a common base class and a bunch of classes
derived from it, and...
|
by: Bit Byter |
last post by:
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...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |