Federico klez Culloca
Federico klez Culloca

Reputation: 27119

Get the list of methods of a class

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

Answers (7)

gomons
gomons

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

Jerry Coffin
Jerry Coffin

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

James Curran
James Curran

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

Preet Sangha
Preet Sangha

Reputation: 65486

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

Upvotes: 2

Artem Barger
Artem Barger

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

JaredPar
JaredPar

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

Mark Ransom
Mark Ransom

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

Related Questions