invoking a method on an object

G

Ganesh Chakka

I am trying to solve a problem in our application. I am at a point
where I want to invoke a method on the object based on the name of the
method.

======================================================
For Example :

From a configuration file I would know that I have to invoke a method
"foo" on object "o".

So, I have 2 strings "foo" and "o" in the code at run time.

I have to now do something to invoke this method "foo" on "o". That is
I have to do o.foo() at runtime.

======================================================

I know this is possible in java using reflection. Does any one know
how I can do this in C++?

Thanks in advance for any help.

-Ganesh.
 
S

Sharad Kala

Ganesh Chakka said:
I am trying to solve a problem in our application. I am at a point
where I want to invoke a method on the object based on the name of the
method.

======================================================
For Example :

From a configuration file I would know that I have to invoke a method
"foo" on object "o".

So, I have 2 strings "foo" and "o" in the code at run time.

I have to now do something to invoke this method "foo" on "o". That is
I have to do o.foo() at runtime.

======================================================

I know this is possible in java using reflection. Does any one know
how I can do this in C++?

One possible way is -
1. Create objects using the factory pattern.
2. Maintain a map of string and pointer to member function.
typedef void (A::*foofn)();
map<string, foofn> a;
3. Invoke methods using the lookup in map.

Ofcourse this method has limitations.

-Sharad
 

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
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top