Connecting Tech Pros Worldwide Forums | Help | Site Map

how to implement a simple class forname?

Manuel
Guest
 
Posts: n/a
#1: Jul 19 '06
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 the same
type.
So what type to specify in the declaration of the map?

map<std::string,void *¿?

PD: excuse me for my bad english


Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 19 '06

re: how to implement a simple class forname?


Manuel wrote:
Quote:
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.
Member of what class? What arguments does it take? What type does
it return?
Quote:
The problem is that the map need that the object saved are the same
type.
Right.
Quote:
So what type to specify in the declaration of the map?
The type of the objects you intend to store in the map.
Quote:
map<std::string,void *¿?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Manuel
Guest
 
Posts: n/a
#3: Jul 19 '06

re: how to implement a simple class forname?


Victor Bazarov wrote:
Quote:
Manuel wrote:
Quote:
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.
>
Member of what class? What arguments does it take? What type does
it return?
The members are of different class that derive of a superclass and this
members do
not return anything.
Quote:
>
Quote:
The problem is that the map need that the object saved are the same
type.
>
Right.
>
Quote:
So what type to specify in the declaration of the map?
>
The type of the objects you intend to store in the map.
>
Quote:
map<std::string,void *¿?
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks for your fast reply

Victor Bazarov
Guest
 
Posts: n/a
#4: Jul 19 '06

re: how to implement a simple class forname?


Manuel wrote:
Quote:
Victor Bazarov wrote:
Quote:
>Manuel wrote:
Quote:
>>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.
>>
>Member of what class? What arguments does it take? What type does
>it return?
>
The members are of different class that derive of a superclass and
this members do
not return anything.
What's the point to have member functions of different classes stored
in the same container? What's _common_ about those functions? How
do you intend to use those map elements?

And if they don't return anything, the return value type is 'void'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Manuel
Guest
 
Posts: n/a
#5: Jul 19 '06

re: how to implement a simple class forname?



Victor Bazarov wrote:
Quote:
Manuel wrote:
Quote:
Victor Bazarov wrote:
Quote:
Manuel wrote:
>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.
>
Member of what class? What arguments does it take? What type does
it return?
The members are of different class that derive of a superclass and
this members do
not return anything.
>
What's the point to have member functions of different classes stored
in the same container? What's _common_ about those functions? How
do you intend to use those map elements?
>
And if they don't return anything, the return value type is 'void'.
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
The methods are used to get objects. This methods have the same name
and the same parameters. it is like a constructor.
I have got a xml file that specifies the objects that i want with his
state

In this file I have a label with the name of the class and the values
of his attributes.
So I parse the file and create the corresponding objects.

Manuel
Guest
 
Posts: n/a
#6: Jul 19 '06

re: how to implement a simple class forname?



Victor Bazarov wrote:
Quote:
Manuel wrote:
Quote:
Victor Bazarov wrote:
Quote:
Manuel wrote:
>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.
>
Member of what class? What arguments does it take? What type does
it return?
The members are of different class that derive of a superclass and
this members do
not return anything.
>
What's the point to have member functions of different classes stored
in the same container? What's _common_ about those functions? How
do you intend to use those map elements?
>
And if they don't return anything, the return value type is 'void'.
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
The methods are used to get objects. This methods have the same name
and the same parameters. it is like a constructor.
I have got a xml file that specifies the objects that i want with his
state

In this file I have a label with the name of the class and the values
of his attributes.
So I parse the file and create the corresponding objects.

Thomas J. Gritzan
Guest
 
Posts: n/a
#7: Jul 19 '06

re: how to implement a simple class forname?


Manuel schrieb:
Quote:
Victor Bazarov wrote:
Quote:
>Manuel wrote:
Quote:
>>Victor Bazarov wrote:
>>>Manuel wrote:
>>>>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.
>>>Member of what class? What arguments does it take? What type does
>>>it return?
>>The members are of different class that derive of a superclass and
>>this members do
>>not return anything.
>What's the point to have member functions of different classes stored
>in the same container? What's _common_ about those functions? How
>do you intend to use those map elements?
>>
>And if they don't return anything, the return value type is 'void'.
>>
>V
>--
>Please remove capital 'A's when replying by e-mail
>I do not respond to top-posted replies, please don't ask
>
The methods are used to get objects. This methods have the same name
and the same parameters. it is like a constructor.
I have got a xml file that specifies the objects that i want with his
state
So the methods are static functions?
That's an information we need to help you. Give an example of such a
class/function (a minimal example).

You can typedef your function type and put pointers to that in the map:

typedef void factory_t(baseclass&, const parameters&);

std::map< std::string, factory_t* your_map;

--
Thomas
Manuel
Guest
 
Posts: n/a
#8: Jul 19 '06

re: how to implement a simple class forname?


Thomas J. Gritzan wrote:
Quote:
Manuel schrieb:
Quote:
Victor Bazarov wrote:
Quote:
Manuel wrote:
>Victor Bazarov wrote:
>>Manuel wrote:
>>>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.
>>Member of what class? What arguments does it take? What type does
>>it return?
>The members are of different class that derive of a superclass and
>this members do
>not return anything.
What's the point to have member functions of different classes stored
in the same container? What's _common_ about those functions? How
do you intend to use those map elements?
>
And if they don't return anything, the return value type is 'void'.
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
The methods are used to get objects. This methods have the same name
and the same parameters. it is like a constructor.
I have got a xml file that specifies the objects that i want with his
state
>
So the methods are static functions?
That's an information we need to help you. Give an example of such a
class/function (a minimal example).
>
You can typedef your function type and put pointers to that in the map:
>
typedef void factory_t(baseclass&, const parameters&);
>
std::map< std::string, factory_t* your_map;
>
--
Thomas
well Thomas, you give me an idea to solve the problem.

i can create a static method in the factory class for each one of the
classes and then in the map it save the pointer to this methods.

So, when I need a object i call to factory::(*pointer)(const
paramaters&)

something as well as this:

typedef float (*MyFuncPtrType)(const paramaters&);

map<std::string, MyFuncPtrType>;

map["method1"] = &method1;
map["method2"] = &method2;

when i need a object i call: factory::(*map["method1"])(parameters).

I think that this can work. what do you think?

Victor Bazarov
Guest
 
Posts: n/a
#9: Jul 19 '06

re: how to implement a simple class forname?


Manuel wrote:
Quote:
Thomas J. Gritzan wrote:
Quote:
>Manuel schrieb:
Quote:
>>Victor Bazarov wrote:
>>>Manuel wrote:
>>>>Victor Bazarov wrote:
>>>>>Manuel wrote:
>>>>>>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.
>>>>>Member of what class? What arguments does it take? What type
>>>>>does it return?
>>>>The members are of different class that derive of a superclass and
>>>>this members do
>>>>not return anything.
>>>What's the point to have member functions of different classes
>>>stored in the same container? What's _common_ about those
>>>functions? How do you intend to use those map elements?
>>>>
>>>And if they don't return anything, the return value type is 'void'.
>>>>
>>>V
>>>--
>>>Please remove capital 'A's when replying by e-mail
>>>I do not respond to top-posted replies, please don't ask
>>>
>>The methods are used to get objects. This methods have the same name
>>and the same parameters. it is like a constructor.
>>I have got a xml file that specifies the objects that i want with
>>his state
>>
>So the methods are static functions?
> That's an information we need to help you. Give an example of such a
>class/function (a minimal example).
>>
>You can typedef your function type and put pointers to that in the
>map:
>>
>typedef void factory_t(baseclass&, const parameters&);
>>
>std::map< std::string, factory_t* your_map;
>>
>--
>Thomas
>
well Thomas, you give me an idea to solve the problem.
>
i can create a static method in the factory class for each one of the
classes and then in the map it save the pointer to this methods.
>
So, when I need a object i call to factory::(*pointer)(const
paramaters&)
>
something as well as this:
>
typedef float (*MyFuncPtrType)(const paramaters&);
>
map<std::string, MyFuncPtrType>;
>
map["method1"] = &method1;
map["method2"] = &method2;
>
when i need a object i call: factory::(*map["method1"])(parameters).
>
I think that this can work. what do you think?
If I may... This sounds OK, only to take address of a member function
you need the class before the name:

map["method1"] = &OneClass::method1;
map["method2"] = &TwoClass::method2;

And when you call it, you can't prepend it with 'factory::'. You just
use

map["method1"](parameters);

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Manuel
Guest
 
Posts: n/a
#10: Jul 19 '06

re: how to implement a simple class forname?


Victor Bazarov wrote:
Quote:
If I may... This sounds OK, only to take address of a member function
you need the class before the name:
>
map["method1"] = &OneClass::method1;
map["method2"] = &TwoClass::method2;
>
And when you call it, you can't prepend it with 'factory::'. You just
use
>
map["method1"](parameters);
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Of course, you may.

well. I think that this is not neccesary because in the end the methods
will be of the same class.

i make this:

FACTORY_H

typedef void (*PtrMethod)(std::vector<std::string>);

class Factory
{
private:
map<const char*, PtrMethod*, ltstrtheMap;

protected:
Factory();

~Factory();

Object * method1(std::vector<std::string>);
Object * method2(std::vector<std::string>);

public:
Object *
getObject(std::string,std::vector<std::string>);
};

FACTORY_CPP

Factory::Factory()
{
map["method1"] = &method1;
map["method2"] = &method2;
}

Object * Factory::getObject(string label,vector<stringparamaters)
{
PtrMethod method = theMap[label];
Object * object = (*method)(parameters);
return energia;
}

is it correct? what do you think?

thank you very much by your attention

Victor Bazarov
Guest
 
Posts: n/a
#11: Jul 19 '06

re: how to implement a simple class forname?


Manuel wrote:
Quote:
[..]
well. I think that this is not neccesary because in the end the
methods will be of the same class.
>
i make this:
>
FACTORY_H
>
typedef void (*PtrMethod)(std::vector<std::string>);
>
class Factory
{
private:
map<const char*, PtrMethod*, ltstrtheMap;
>
protected:
Factory();
>
~Factory();
>
Object * method1(std::vector<std::string>);
Object * method2(std::vector<std::string>);
>
public:
Object *
getObject(std::string,std::vector<std::string>);
};
>
FACTORY_CPP
>
Factory::Factory()
{
map["method1"] = &method1;
map["method2"] = &method2;
}
>
Object * Factory::getObject(string label,vector<stringparamaters)
{
PtrMethod method = theMap[label];
Object * object = (*method)(parameters);
return energia;
}
>
is it correct? what do you think?
Close, but incorrect. If your 'method1' and 'method2' return Object*,
you cannot declare 'PtrMethod' as returning "void". You have to make
sure the type is the same as the functions. That's the glaring error.

Then 'Factory::getObject' returns 'energia' where it should probably
return 'object'.

Not so important ones include passing by value where passing by const
reference should be sufficient.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Manuel
Guest
 
Posts: n/a
#12: Jul 20 '06

re: how to implement a simple class forname?



Victor Bazarov wrote:
Quote:
Manuel wrote:
Quote:
[..]
well. I think that this is not neccesary because in the end the
methods will be of the same class.

i make this:

FACTORY_H

typedef void (*PtrMethod)(std::vector<std::string>);

class Factory
{
private:
map<const char*, PtrMethod*, ltstrtheMap;

protected:
Factory();

~Factory();

Object * method1(std::vector<std::string>);
Object * method2(std::vector<std::string>);

public:
Object *
getObject(std::string,std::vector<std::string>);
};

FACTORY_CPP

Factory::Factory()
{
map["method1"] = &method1;
map["method2"] = &method2;
}

Object * Factory::getObject(string label,vector<stringparamaters)
{
PtrMethod method = theMap[label];
Object * object = (*method)(parameters);
return energia;
}

is it correct? what do you think?
>
Close, but incorrect. If your 'method1' and 'method2' return Object*,
you cannot declare 'PtrMethod' as returning "void". You have to make
sure the type is the same as the functions. That's the glaring error.
>
Then 'Factory::getObject' returns 'energia' where it should probably
return 'object'.
>
Not so important ones include passing by value where passing by const
reference should be sufficient.
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oh, you are rigth. I had an error: "PtrMethod" return "Object *" and
"Factory::getObject" returns a Object*. I'm mistaken when copying the
source.

and too, you are rigth with passing by value or by const reference. I
will do it thus

Thank you very much for your help.

Closed Thread


Similar C / C++ bytes