简体   繁体   English

单个函数指针可以指向多个类的成员函数

[英]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: 如果BC都从公共基类继承,则可以将指针传递给该基类并在其上调用函数:

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::functionstd::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.

相关问题 function 指针指向一个成员 function - function pointer point to a member function 可以指向多个类的指针 - Pointer that can point to multiple classes 成员 function 指针可以引用几个不同类之一的方法吗? - Can a member function pointer refer to a method of one of several different classes? 类型双关语问题:不能重新解释从多个类派生到虚拟成员 function 指针的 class 的成员 function 指针 - Type punning issue: Can't reinterpret_cast member function pointer of class which derives from multiple classes into dummy member function pointer 为什么指向非const成员函数的指针不能指向const成员函数而相反? - Why pointer to non-const member function can't point const member function and opposite? 成员函数指针指向另一个对象的方法 - Member function pointer point to method of another object 将乘法类的指针传递给成员函数 - passing pointer to multiply classes to a member function 相同的通用 function 可以接收指向成员函数和常规函数的指针作为单个参数吗? - Can the same generic function receive a pointer to both a member-function and a regular-function as a single argument? 排序比较函数可以作为成员函数的指针吗? - Can sort comparison function be a pointer on a member function? Function class 内的指针指向该类的成员 function - Function pointer inside class point to that class's member function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM