0

这是我的代码...

struct Test {
  string foo() { return "bar"; }
};

#define callFn(obj,method) obj->method();

int main() {
  Test* t = new Test();
  cout << callFn(t,foo); // bar
  return 0;
}

...这是我想要的代码

int main() {
  Test* t = new Test();
  string method = "foo";
  cout << callFn(t,method); // bar
  return 0;
}

可能吗?

4

4 回答 4

3

你不能。C++ 没有反射功能。

您必须定义例如std::map将字符串映射到函数指针的 a。

void foo(int x) { std::cout << "foo " << (x+3) << "\n"; }
void bar(int x) { std::cout << "bar " << (x+5) << "\n"; }

int main() {
    std::map<std::string, void (*)(int)> mapper;
    mapper["foo"] = &foo;
    mapper["bar"] = &bar;

    // ...

    mapper["foo"](42);
    mapper["bar"](42);
}
于 2012-05-19T19:46:28.073 回答
1

您可能想要成员函数指针之类的东西:

typedef std::string (Test::*ptmf)();

#define CALL_MF(pobject, p) (((pobject)->*(p))())

int main()
{
    ptmf method = &Test::foo;
    Test * t = new Test;
    std::string result = CALL_MF(t, method);   // or directly: (t->*method)()
}

您可以创建其元素类型ptmf为的容器,以在运行时管理不同的成员函数指针:

std::map<int, ptmf> function_registry;

std::string call(int key, Test * t)
{
    auto it = function_registry.find(key);
    return (it != function_registry.end()) ? CALL_MF(t, *it) : "[ERROR]";
}
于 2012-05-19T19:50:23.413 回答
1

你可以做这样的事情,但是因为 C++ 缺乏反射能力,你必须做一些额外的工作才能使它成为可能。

struct base {
  virtual void call_method( std::string const & ) = 0;
};

struct derived : public base {
  std::string foo( ) const {
    return "bar";
  }

  // More methods.

  void call_method( std::string const &p_name ) {
    if( p_name == "foo" ) {
      this -> foo( );
    }

    // More checks on method names.

    else {
      // Handle invalid function name.
    }
  }
};

这称为数据驱动接口,您可以在其中将命令传递给对象,它们会以多态方式响应它们识别的命令。您可以通过创建从命令到函数指针的静态初始化无序映射然后使用它来解析要调用的函数来改进我展示的内容。但是,如果可以的话,最好避免这种类型的函数调度,因为与静态函数调度相比它很慢并且容易出错,因为拼写错误可能导致不正确的调用或错误。它还有一个缺点,即您无法轻松获得返回值,尽管在某些情况下是可能的。

编辑:我想给出一个更完整的例子来说明如何做到这一点,所以这里是:

#include <cassert>
#include <iostream>

#include <boost/bind.hpp>
#include <boost/blank.hpp>
#include <boost/variant.hpp>
#include <boost/function.hpp>

#include <boost/unordered_map.hpp>
#include <boost/assign/list_of.hpp>

// A base class that defines an interface to call methods by name
// and to access the list of methods.  We use a map of argument
// names to boost::variants to pass arguments to the functions.
// Right now we support only ints and strings, but we can expand
// this to other types if we want.  In particular, we can use
// boost::any to support arbitrary types, but it will be slow.
// Maybe that's not a big deal since function dispatch through
// named functions is slow anyway.

struct base {
  typedef boost::variant< boost::blank, int, std::string > argument_t;
  typedef boost::variant< boost::blank, int, std::string >   return_t;

  typedef boost::unordered_map< std::string, argument_t > param_map_t;

  typedef boost::function< return_t ( base *, param_map_t const & ) >
  method_t;

  typedef boost::unordered_map< std::string, method_t > method_map_t;

  return_t call_method(
      std::string const &p_method
    , param_map_t const &p_params = param_map_t( )
  )
  {
    method_map_t::const_iterator l_itr =
      get_methods( ).find( p_method );

    if( l_itr == get_methods( ).end( )) {
      // Handle undefined method identifier.
    }

    return l_itr -> second( this, p_params );
  }

  virtual method_map_t const &get_methods( ) const = 0;
};

// A trampoline object to elide the concrete type that
// implements the base interface and to provide appropriate
// casting.  This is necessary to force all functions in our
// method map to have the same type.

template< typename U >
base::return_t trampoline(
    base::return_t (U::*p_fun)( base::param_map_t const & )
  , base *p_obj
  , base::param_map_t const &p_param_map
)
{
  U *l_obj = static_cast< U* >( p_obj );
  return (l_obj ->* p_fun)( p_param_map );
}

// A derived type that implements the base interface and
// provides a couple functions that we can call by name.

struct derived : public base {
  static method_map_t const c_method_map;

  return_t foo( param_map_t const &p_params ) {
    std::cout << "foo" << std::endl;  return 1;
  }

  return_t bar( param_map_t const &p_params ) {
    std::cout << "bar" << std::endl;  return std::string( "bar" );
  }

  method_map_t const &get_methods( ) const {
    return c_method_map;
  }
};

// Construct map of method names to method pointers for derived.

base::method_map_t const derived::c_method_map = boost::assign::map_list_of
  ( "foo", boost::bind( &trampoline< derived >, &derived::foo, _1, _2 ))
  ( "bar", boost::bind( &trampoline< derived >, &derived::bar, _1, _2 ))
;

int main( ) {
  base *blah = new derived( );

  // Call methods by name and extract return values.

  assert( boost::get< int         >( blah -> call_method( "foo" )) == 1     );
  assert( boost::get< std::string >( blah -> call_method( "bar" )) == "bar" );

  // Iterate over available methods

  typedef base::method_map_t::const_iterator iterator;

  iterator l_itr = blah -> get_methods( ).begin( );
  iterator l_end = blah -> get_methods( ).end  ( );

  for( ; l_itr != l_end; ++l_itr ) {
    if( l_itr -> first == "foo" ) l_itr -> second( blah, base::param_map_t( ));
  }
}

输出是:

foo
bar
foo

如您所见,设置它需要做很多工作,但是添加实现接口的新类型非常容易。

于 2012-05-19T20:00:26.953 回答
0

这本质上是 Java1.5 后可用的反射机制

这是 C++ 中的反射示例

http://www.garret.ru/cppreflection/docs/reflect.html

于 2012-05-19T19:53:41.847 回答