tags:

views:

84

answers:

6

Is there a way to create a list (e.g. an array) of pointers to each method of a C++ class?

Something like Type.GetMethods() in the .NET framework, but using only standard C++.

+1  A: 

Make a copy of the .h file and hack away with it in an editor.

No, there's no way to do it automatically.

Mark Ransom
+2  A: 

No this is not possible in a general way. C++ does not have the same metadata infrastructure that .Net posses.

Could you provide us with a scenario where you want to use this information? There may be a better approach you can use with C++

JaredPar
By now it was just out of curiosity. I mean, yesterday I was thinking about some sort of library to handle JSON data, but today I can't remember why I thought about this approach -_- . Still the question remained, so I asked.
klez
it's a good illustration of how different the languages are even if the syntax is similar-looking. The mentality of how to solve problems is entirely different between the two languages.
tenfour
A: 

There is no meta classes in C++ only objects and classes, hence no reflection could not be involved, so the answer is no.

Artem Barger
A: 

You can get the type information using typeid but not the method information.

Preet Sangha
+1  A: 

There is no way.

In fact, even at the object code level, a static class member function cannot be distinguished from a stand-alone function, nor a class instance function from a stand-alone function passing a pointer to an object.

If you know your compilers name-mangling scheme, and had access to the pre-linked object code, you might be able to decode it, but that's a lot of work for iffy results.

James Curran
+1 for the hardcore "get 'er done" approach.
tenfour
A: 

If you really want to do this, chapter 8 of Advanced C++ Programming Styles and Idioms by James Coplien (probably long-since out of print, but I hear Neil would be willing to sell his copy cheaply) covers programming with "exemplars" in C++. Make not mistake, the capability doesn't come for free, but it can/does provide metaclass-like capabilities in C++.

Offhand, I don't remember his building the particular capability you're looking for into one of his classes, but it's been a long time since I read that book carefully at all either. It's always possible that it couldn't be done at all, but I think it would work, if you can live with the other limitations of what he discloses.

Jerry Coffin