![]() |
Re: Urgent! how to get object name, method name and attribute name based on the strings?
tom_usenet@hotmail.com (tom_usenet) wrote in message news:<3ef72572.22695734@news.easynet.co.uk>...
> On 23 Jun 2003 06:28:31 -0700, d.f.zhou@lboro.ac.uk (ding feng) wrote: > > >I have got a list of string for object name, method name and attribute > >name, respectively. for example, vector<string> objname[10], > >vector<string>methodname[10]; vector<string> attrname<10>. > > > >when i call a function in the main, I pass the strings as parameters, > >for example, func1(objname[0], methodname[1], attrname[5]), then this > >function needs to work as follows: > >objname[0].methodname[1](attrname[5]). Since each parameter is string, > >it is not able to call object method, I wonder how to do that? > > You have to build up a map of object name to object, method name to > method pointer. e.g. > #include <string> > #include <map> > #include <iostream> > > using namespace std; > > class Foo > { > int i; > public: > Foo(int i): i(i){} > > void print1(string s) > { > cout << "print1(" << s > << ") called on Foo(" << i << ")\n"; > } > void print2(string s) > { > cout << "print2(" << s > << ") called on Foo(" << i << ")\n"; > } > }; > > map<string, Foo*> objectmap; > > map<string, void (Foo::*)(string)> methodmap; > > void invoke(string object, string method, string argument) > { > (objectmap[object]->*methodmap[method])(argument); > } > > int main() > { > Foo foo1(1); > Foo foo2(2); > objectmap["foo1"] = &foo1; > objectmap["foo2"] = &foo2; > > methodmap["print1"] = &Foo::print1; > methodmap["print2"] = &Foo::print2; > > invoke("foo2", "print1", "first call"); > invoke("foo1", "print2", "second call"); > } > > If you need anything more complicated, the code gets much more > complicated. > > Tom Thanks for your advice. When I ran this program, there are about 215 warnings. dingfeng |
Re: Urgent! how to get object name, method name and attribute name based on the strings?
On 24 Jun 2003 06:55:45 -0700, d.f.zhou@lboro.ac.uk (ding feng) wrote:
>Thanks for your advice. When I ran this program, there are about 215 warnings. You're using VC6 then? See: http://support.microsoft.com/default...b;EN-US;122539 and, for a thread detailing the solution: http://www.xsls.com/?494 Tom |
Re: Urgent! how to get object name, method name and attribute name based on the strings?
yes, I use VC6.
d.f.zhou@lboro.ac.uk (ding feng) wrote in message news:<42fa0350.0306240555.6ce6a1b2@posting.google. com>... > tom_usenet@hotmail.com (tom_usenet) wrote in message news:<3ef72572.22695734@news.easynet.co.uk>... > > On 23 Jun 2003 06:28:31 -0700, d.f.zhou@lboro.ac.uk (ding feng) wrote: > > > > >I have got a list of string for object name, method name and attribute > > >name, respectively. for example, vector<string> objname[10], > > >vector<string>methodname[10]; vector<string> attrname<10>. > > > > > >when i call a function in the main, I pass the strings as parameters, > > >for example, func1(objname[0], methodname[1], attrname[5]), then this > > >function needs to work as follows: > > >objname[0].methodname[1](attrname[5]). Since each parameter is string, > > >it is not able to call object method, I wonder how to do that? > > > > You have to build up a map of object name to object, method name to > > method pointer. e.g. > > #include <string> > > #include <map> > > #include <iostream> > > > > using namespace std; > > > > class Foo > > { > > int i; > > public: > > Foo(int i): i(i){} > > > > void print1(string s) > > { > > cout << "print1(" << s > > << ") called on Foo(" << i << ")\n"; > > } > > void print2(string s) > > { > > cout << "print2(" << s > > << ") called on Foo(" << i << ")\n"; > > } > > }; > > > > map<string, Foo*> objectmap; > > > > map<string, void (Foo::*)(string)> methodmap; > > > > void invoke(string object, string method, string argument) > > { > > (objectmap[object]->*methodmap[method])(argument); > > } > > > > int main() > > { > > Foo foo1(1); > > Foo foo2(2); > > objectmap["foo1"] = &foo1; > > objectmap["foo2"] = &foo2; > > > > methodmap["print1"] = &Foo::print1; > > methodmap["print2"] = &Foo::print2; > > > > invoke("foo2", "print1", "first call"); > > invoke("foo1", "print2", "second call"); > > } > > > > If you need anything more complicated, the code gets much more > > complicated. > > > > Tom > > > Thanks for your advice. When I ran this program, there are about 215 warnings. > > dingfeng |
| All times are GMT. The time now is 09:24 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.