Click to See Complete Forum and Search --> : dynamic function ptr lookup


nex
October 3rd, 2002, 07:00 AM
Hey,

Just wondering as I have been writing applications in Java for a couple years now and recently gone back to developing with c++.

With Java I can easily find a method during run-time with the classes Method and Class, by just having Strings with the class name and method name.

Is something similar possible in c++? If so, how would it affect performance?


thanks,

/ nex

PaulWendt
October 3rd, 2002, 07:46 AM
There is no reflection mechanism in C++. The closest thing would
have to be RTTI, but that's nowhere near the flexibility of
reflection. You'd have to store tables of function pointers and
do it yourself in C++.

--Paul

Yves M
October 3rd, 2002, 08:42 AM
I have never used this feature in Java. In which situations might you need reflection ?

PaulWendt
October 3rd, 2002, 08:58 AM
I am pretty sure that Remote Object instantiation [similar to COM]
requires reflection. With reflection, you can take any Object and
iterate through all of its member functions and properties. I think
that JavaBeans have to have certain functions; reflection can let
you determine whether or not an Object is a JavaBean.

It has some other uses too. We were doing a custom workflow
program where each "node" was a separate java-class that was
generated on the fly. We used reflection to query each object's
capabilities [I believe. It's been a while so I don't remember all
of the specifics any longer].

--Paul

nex
October 3rd, 2002, 09:04 AM
This functionality I find great. Specially when developing server-side applications.

This makes it very easy to use one single entry point for client requests. The client just has to send the method's name to the server and instead of going through a long switch-case it can directly go to the right method on the server which will send (hopefully) the right response to the client.

When adding more functions and classes I do not have to change anything in the server's core. Just add a new class, compile and then the client can access the new method.

Or when developing a application where I want to be able to add new functionality by just creating a new class, adding the class/method name in a xml-file and the application can use this new functionality... again without having to add code in the application's core.

For instance a user's input could directly call the right function, if it is specified in the xml.

/nex

PaulWendt
October 3rd, 2002, 09:47 AM
Yeah nex is right about that :)
We have another project that is a client-server application where
we do just that as well.

--Paul

Yves M
October 3rd, 2002, 09:55 AM
Interesting, it is defintely useful then. Paul, do you know whether C# or managed code add this feature as well ?

PaulWendt
October 3rd, 2002, 10:22 AM
I'm sorry; I currently have no knowledge of anything C# related.
I work at a place that supports multiple platforms so learning
C# just hasn't been a priority for me....

--Paul