[英]Can a single function pointer point to multiple classes member function
Here are the requirements posed by my application. 以下是我的申请提出的要求。 I have a class A, that accepts a function pointer say cFunc, Basically in my implementation of A, I have it call cFunc multiple times.
我有一个类A,它接受一个函数指针说cFunc,基本上在我的A实现中,我有多次调用cFunc。
The cFunc pointer itself should point to different functions depending upon the application. cFunc指针本身应根据应用程序指向不同的函数。 Thus for each application I create a class with the same function definition as cFunc, however I cannot assign the class's member function to this pointer
因此,对于每个应用程序,我创建一个具有与cFunc相同的函数定义的类,但是我不能将类的成员函数赋给此指针
class A {
typedef double (*Def_CFunc)(std::vector<double>);
A(Def_CFunc _cFunc) { // Some implementation}
// Other Functions
};
class B { double someFunc(std::vector<double> b); };
class C { double someOtherFunc(std::vector<double> a); };
int main () {
B firstObj;
C secondObj;
// Depending upon the situation, I want to select class B or C
double (*funcPointer)(std::vector<double>) = firstObj.someFunc; // Error in this line of code
A finalObj(funcPointer);
}
So how do I make it such that any class with a member function of the given format can be used to initialize the class A? 那么如何使得任何具有给定格式的成员函数的类都可以用来初始化类A?
I'm not sure what exactly your requirements are, but it looks like you want an interface (or abstract base class in C++ lingo). 我不确定你的要求是什么,但看起来你想要一个接口 (或C ++术语中的抽象基类)。
If both B
and C
inherit from a common base class, you can pass a pointer to this base class and invoke functions on it: 如果
B
和C
都从公共基类继承,则可以将指针传递给该基类并在其上调用函数:
class I { virtual double func(std::vector<double> a) = 0; }
class B : public I { double func(std::vector<double> a); };
class C : public I { double func(std::vector<double> a); };
You can pass an I*
pointer to A
and just use i->func
. 您可以将
I*
指针传递给A
并使用i->func
。
Pointer to member function has different syntax than pointer to ordinary function and can only point to a method of one given class. 指向成员函数的指针具有与指向普通函数的指针不同的语法,并且只能指向一个给定类的方法。 To be able to point to methods in different classes use boost::function or if C++11 is available use std::function .
为了能够指向不同类中的方法,可以使用boost :: function,或者如果C ++ 11可用,则使用std :: function 。 These can hold any method or function of a given signature.
这些可以包含给定签名的任何方法或功能。
你需要的是std::function
和std::bind
或lambda表达式(或前两个的Boost等价物),因为成员函数指针不允许你这样做。
You can do it using std::bind + std::function. 你可以使用std :: bind + std :: function来做到这一点。 Lets write some template class wrapper, that takes any static type as input.
让我们编写一些模板类包装器,它将任何静态类型作为输入。 Then use this wrapper in free function switch_obj.
然后在自由函数switch_obj中使用此包装器。 Usage is very simple.
用法很简单。
typedef std::function<double(std::vector<double>)> my_foo;
template<class C>
struct switcher
{
static my_foo convert(C* obj)
{
return my_foo( std::bind(&C::someFunc,obj,std::placeholders::_1) );
}
};
template<class T>
my_foo switch_obj(T* obj)
{
return switcher<T>::convert(obj);
}
void main()
{
B firstObj;
C secondObj;
auto f = switch_obj(&firstObj);
A a(f);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.