C++ 获取指向对象成员函数的指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8865766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get a pointer to object's member function
提问by Alex Hoppus
Here is the problem:
这是问题所在:
1) I have a class like so:
1)我有一个这样的课程:
class some_class
{
public:
some_type some_value;
int some_function(double *a, double *b, int c, int d, void *e);
};
2) Inside some_function
, I use some_values
from some_class
object to get a result.
2)在里面some_function
,我使用some_values
fromsome_class
对象来得到结果。
3) So, I have a concrete object and I want to get a pointer to this object some_function
.
3) 所以,我有一个具体的对象,我想得到一个指向这个对象的指针some_function
。
Is it possible? I can't use some_fcn_ptr
because the result of this function depends on the concrete some_value
of an object.
是否可以?我无法使用,some_fcn_ptr
因为此函数的结果取决于some_value
对象的具体情况。
How can I get a pointer to some_function
of an object? Thanks.
如何获得指向some_function
对象的指针?谢谢。
typedef int (Some_class::*some_fcn_ptr)(double*, double*, int, int, void*);
回答by Andriy Tylychko
You cannot, at least it won't be only a pointer to a function.
你不能,至少它不会只是一个指向函数的指针。
Member functions are common for all instances of this class. All member functions have the implicit (first) parameter, this
. In order to call a member function for a specific instance you need a pointer to this member function and this instance.
成员函数对于此类的所有实例都是通用的。所有成员函数都有隐式(第一个)参数this
。为了调用特定实例的成员函数,您需要一个指向该成员函数和该实例的指针。
class Some_class
{
public:
void some_function() {}
};
int main()
{
typedef void (Some_class::*Some_fnc_ptr)();
Some_fnc_ptr fnc_ptr = &Some_class::some_function;
Some_class sc;
(sc.*fnc_ptr)();
return 0;
}
More info here in C++ FAQ
C++ 常见问题中的更多信息
Using Boostthis can look like (C++11 provides similar functionality):
使用Boost这看起来像(C++11 提供了类似的功能):
#include <boost/bind.hpp>
#include <boost/function.hpp>
boost::function<void(Some_class*)> fnc_ptr = boost::bind(&Some_class::some_function, _1);
Some_class sc;
fnc_ptr(&sc);
C++11's lambdas:
C++11 的 lambda 表达式:
#include <functional>
Some_class sc;
auto f = [&sc]() { sc.some_function(); };
f();
// or
auto f1 = [](Some_class& sc) { sc.some_function(); };
f1(sc);
回答by Vyktor
You may write some kind of Wrapper which is able to use both function or method as parameter.
您可以编写某种能够同时使用函数或方法作为参数的 Wrapper。
I used following classes for launching functions (it was used in one my SDL program):
我使用以下类来启动函数(它在我的一个 SDL 程序中使用):
class CallbackFunction {
public:
// Constructor, copy constructor and destructor
virtual int execute( SDL_keysym* keysym) const;
virtual int operator()( SDL_keysym* keysym) const;
protected:
int( *callback)( SDL_keysym*));
}
int CallbackFunction::execute( SDL_keysym* keysym) const{
return callback(keysym);
}
int CallbackFunction::operator()( SDL_keysym* keysym) const{
return callback( keysym);
}
And this extension for "methods":
而这个“方法”的扩展:
template<class T>
class CallbackMethod : public CallbackFunction {
public:
// Constructor, copy constructor and destructor
CallbackMethod( T *object, int(T::*callback)( SDL_keysym* keysym));
int execute( SDL_keysym* keysym) const;
int operator()(SDL_keysym* keysym) const;
protected:
T *object;
int(T::*method)( SDL_keysym* keysym);
};
// Object initialization (constructor)
template<class T>
CallbackMethod<T>::CallbackMethod( T *object, int(T::*callback)( SDL_keysym* keysym)):
CallbackFunction( NULL),object(object),method(callback){
}
// Responsible for executing
template<class T>
int CallbackMethod<T>::execute( SDL_keysym* keysym) const {
return (object->*method)(keysym);
}
template<class T>
int CallbackMethod<T>::operator()( keysym) const {
return (object->*method)( keysym);
}
And then use it as:
然后将其用作:
CallbackFunction *callback;
callback = new CallbackFunction( myFunction);
callback = new CallbackMethod<A>( instanceOfA, instanceOfA::myMethod);
callback = new CallbackMethod<B>( instanceOfB, instanceOfB::myMethod);
...
callback( keysym);
I found macro as this:
我发现宏是这样的:
CALLBACK(object,method) new CallbackMethod<typeof(*object)>( object, &method)
really useful
真的很有用
回答by Dov Grobgeld
No, you can't get a pointer to a C++ class method (unless the method is declared static). The reason is that a class method always has the pointer this
, a pointer to the class instance. But were you to call the method through a pointer, that pointer could not encapsulate the this
pointer, and then then there would be no instance attached, and therefore this behavior is not legal.
不,您无法获得指向 C++ 类方法的指针(除非该方法声明为静态)。原因是一个类方法总是有一个指针this
,一个指向类实例的指针。但是如果你通过指针调用方法,那个指针不能封装this
指针,那么就不会有实例附加,因此这种行为是不合法的。
回答by celtschk
While not exactly what you requested, if you can use C++11 the following might nevertheless suit your needs (untested):
虽然不完全符合您的要求,但如果您可以使用 C++11,以下内容可能仍然适合您的需求(未经测试):
std::function<int(double*, double*, int, int, void*)>
some_function_of(Some_class& obj)
{
return [&](double* a, double* b, int c, int d, void* e){
return obj.some_func(a, b, c, d, e); };
}
回答by Zoob
I used the Boostlibrary. I included "boost/bind.hpp" in my code. Then a method named "fn" can be defined as
我使用了Boost库。我在我的代码中包含了“boost/bind.hpp”。那么一个名为“fn”的方法可以定义为
auto fn = boost::bind(ClassName::methodName, classInstanceName, boost::placeholders::_1);
auto fn = boost::bind(ClassName::methodName, classInstanceName, boost::placeholders::_1);