Board index » cppbuilder » Re: Creating a Class from an char* (Convert char * to Type of aclass)

Re: Creating a Class from an char* (Convert char * to Type of aclass)


2006-02-14 03:52:14 AM
cppbuilder109
I understand a little, but itīs difficult to me think the final
implementation. I will study it a little more, itīs interesting what you
told me.
Have you some documentation or an example that helps me a bit thinking
about this?
Chris Uzdavinis (TeamB) escribi?
Quote
Raul Lorenzo < XXXX@XXXXX.COM >writes:

>Hi,
>
>I have the code below, and I want to code it something like this:
>
>/*****************************************/
>Need something like this
>/*****************************************/
>/*This is very bad, but .....*/
>Result=new plugin_name;
>return Result;
>Do you understand what I mean?

C++ doesn't have a built-in way to do that, but you can still
implement it with a little work. The idea is to store "factories".
Given an arbitrary factory, you can ask it to "make me an object", and
it will. So now all the factories implement a common abstract factory
interface, and for each type you wish to create you can instantiate a
derived factory for it. If your objects are default constructable, or
many of them have the same construction signature, a template can be
used to reduce almost all of the hassle of writing these.

Next, you need to have a way to find the factory corresponding to a
specific string. A std::map<string, AbstractFactory>object would be
good here. At startup you would create your factories (one for each
type), and insert the name and factory into the map.

Later, when you need to create an object based on its typename stored
in a string, you query the map for that string, and if you get back a
factory, you call the create() member function on it, and get back an
instance of the object you needed to have created.

Only problem, if you consider it that, is that all the things the
factories return must inherit from a common base class.

 
 

Re:Re: Creating a Class from an char* (Convert char * to Type of aclass)

Raul Lorenzo < XXXX@XXXXX.COM >writes:
Quote
I understand a little, but itīs difficult to me think the final
implementation. I will study it a little more, itīs interesting what
you told me. Have you some documentation or an example that helps
me a bit thinking about this?
This is a design pattern, named the Abstract Factory, as documented in
the book "Design Patterns" by Gamma, Helm, Johnson, and Vlissides.
If you don't have access to the book, you can about 11 million hits on
google if you search for "abstract factory".
--
Chris (TeamB);
 

Re:Re: Creating a Class from an char* (Convert char * to Type of aclass)

Chris Uzdavinis (TeamB) escribi?
Quote
Raul Lorenzo < XXXX@XXXXX.COM >writes:

>Hi,
>
>I have the code below, and I want to code it something like this:
>
>/*****************************************/
>Need something like this
>/*****************************************/
>/*This is very bad, but .....*/
>Result=new plugin_name;
>return Result;
>Do you understand what I mean?

C++ doesn't have a built-in way to do that, but you can still
implement it with a little work. The idea is to store "factories".
Given an arbitrary factory, you can ask it to "make me an object", and
it will. So now all the factories implement a common abstract factory
interface, and for each type you wish to create you can instantiate a
derived factory for it. If your objects are default constructable, or
many of them have the same construction signature, a template can be
used to reduce almost all of the hassle of writing these.
I should learn to make templates, i think.
Will this template the same for any kind (type) of factory? So, I only
need to code once. isnīt?
For example, I have 2 DLL, the first has 2 plugins and second has 3
plugins. will each DLL stores the factories they need or simply each DLL
has the method to search the factory needed returnig it?
Quote

Next, you need to have a way to find the factory corresponding to a
specific string. A std::map<string, AbstractFactory>object would be
good here. At startup you would create your factories (one for each
type), and insert the name and factory into the map.
My intuition tells me that each DLL should have a template like you told
me and has to initialize the map. true?
 

{smallsort}