Reputation: 27119
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++.
Upvotes: 15
Views: 23349
Reputation: 1976
In my project, I use special macros for class members declaration and definition, then I can get the list of class members. For example:
Class declaration:
#define DECLARE_MODULE_FUNCTION( function_name ) \
JsonObject function_name( JsonObject value );
#define DEFINE_MODULE_FUNCTION( function_name ) \
static ModuleFunctionAdder< LotteryOddsModule > \
__LINE__##function_name( L ## #function_name , &LotteryOddsModule::function_name ); \
JsonObject LotteryOddsModule::function_name( JsonObject value )
template< typename T >
class ModuleFunctionAdder;
class LotteryOddsModule
{
public:
typedef JsonObject ( LotteryOddsModule::*ModuleFunction )( JsonObject );
JsonValue Invoke( JsonValue json_value );
DECLARE_MODULE_FUNCTION( GenerateK1AndK2 );
private:
friend class ModuleFunctionAdder< LotteryOddsModule >;
static std::map< WString , ModuleFunction > _module_functions;
};
template<>
class ModuleFunctionAdder< LotteryOddsModule >
{
public:
ModuleFunctionAdder( WString func_name , LotteryOddsModule::ModuleFunction func )
{
LotteryOddsModule::_module_functions[ func_name ] = func;
}
};
Class definition:
JsonValue LotteryOddsModule::Invoke( JsonValue json_value )
{
return ( this->*_module_functions[ L"GenerateK1AndK2" ] ) ( json_value.get_obj() );
}
DEFINE_MODULE_FUNCTION( GenerateK1AndK2 )
{
//...
}
Upvotes: 1
Reputation: 490108
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.
Upvotes: 1
Reputation: 103495
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.
Upvotes: 3
Reputation: 65486
You can get the type information using typeid but not the method information.
Upvotes: 2
Reputation: 41222
There is no meta classes in C++ only objects and classes, hence no reflection could not be involved, so the answer is no.
Upvotes: 0
Reputation: 754585
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++
Upvotes: 11
Reputation: 308121
Make a copy of the .h file and hack away with it in an editor.
No, there's no way to do it automatically.
Upvotes: 2