how to implement a simple class forname?

M

Manuel

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
 
V

Victor Bazarov

Manuel said:
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 problem is that the map need that the object saved are the same
type.
Right.

So what type to specify in the declaration of the map?

The type of the objects you intend to store in the map.
map<std::string,void *> ¿?

V
 
M

Manuel

Victor said:
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.
The type of the objects you intend to store in the map.


V

Thanks for your fast reply
 
V

Victor Bazarov

Manuel said:
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
 
M

Manuel

Victor said:
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

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.
 
M

Manuel

Victor said:
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

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.
 
T

Thomas J. Gritzan

Manuel said:
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;
 
M

Manuel

Thomas said:
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;

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?
 
V

Victor Bazarov

Manuel said:
Thomas said:
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;

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
 
M

Manuel

Victor said:
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

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*, ltstr> theMap;

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<string> paramaters)
{
PtrMethod method = theMap[label];
Object * object = (*method)(parameters);
return energia;
}

is it correct? what do you think?

thank you very much by your attention
 
V

Victor Bazarov

Manuel said:
[..]
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*, ltstr> theMap;

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<string> paramaters)
{
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
 
M

Manuel

Victor said:
Manuel said:
[..]
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*, ltstr> theMap;

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<string> paramaters)
{
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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top