By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,899 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,899 IT Pros & Developers. It's quick & easy.

how to implement a simple class forname?

P: n/a
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

Jul 19 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 19 '06 #2

P: n/a
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.
>
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
--
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

Jul 19 '06 #3

P: n/a
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
Jul 19 '06 #4

P: n/a

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

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.

Jul 19 '06 #5

P: n/a

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

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.

Jul 19 '06 #6

P: n/a
Manuel schrieb:
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
Jul 19 '06 #7

P: n/a
Thomas J. Gritzan wrote:
Manuel schrieb:
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?

Jul 19 '06 #8

P: n/a
Manuel wrote:
Thomas J. Gritzan wrote:
>Manuel schrieb:
>>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
Jul 19 '06 #9

P: n/a
Victor Bazarov wrote:
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

Jul 19 '06 #10

P: n/a
Manuel wrote:
[..]
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
Jul 19 '06 #11

P: n/a

Victor Bazarov wrote:
Manuel wrote:
[..]
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.

Jul 20 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.