【问题标题】:Storing the function pointers in vector of function pointers将函数指针存储在函数指针向量中
【发布时间】:2021-06-03 19:42:27
【问题描述】:

我正在编写代码以将函数指针存储在函数指针向量中。但在我的实现中,我要存储在向量中的函数是一个类的成员函数,它作为指针存储在另一个 std::map 中。下面是代码sn-p。

class img_preprocess(){ 
public:
   std::vector<void(*)(int)> receivers;
}

class detector(){
public:
   void push(int val) {
      //some code here
   }


class tracker(){
private:
   std::map< int, img_preprocess* > img_actors;
   std::map< int, detector* > detect_actors;

public:
   tracker(){
      this->img_actors.insert({ 1, new img_preprocess() });
      this->detect_actors.insert({ 1, new detector() });

      // adding the detector::push function to img_preprocess receivers vector
      this->img_actors[1]->receivers.push_back(
          & ( this->detect_actors[1]->push )       // this line gives me the error
      )
   }
}

我的意图是在 img_preprocess 对象的接收器向量内为检测器对象的 push 函数保留一个函数指针。上述方法为我提供了 一个指向绑定函数的指针仅用于调用函数错误。关于如何克服这个错误并实现我的意图的任何想法?

编辑 01:

在我的例子中,我必须在接收器向量中存储几个推送函数。这些推送函数是检测器等类的成员(例如:matcher::push、distributor::push)

class Matcher{
public:
   void push(int val){
      //some code here
   }
}

class Distributor{
public:
   void push(int val){
      //some code here
   }
}

【问题讨论】:

  • 松散相关,您可能对type erasure感兴趣。
  • 指向成员函数的指针非常不同。已经有很多重复了。如果不需要访问对象数据,则将函数设为静态,或者您必须将对象附加到它

标签: c++ c++11 pointers function-pointers member-function-pointers


【解决方案1】:

使用std::function。 阅读更多关于here

【讨论】:

    【解决方案2】:

    C++ 中的类定义不应该是这样的:class img_preprocess()。前面的 () 会触发语法错误。非继承、非模板化的类定义通常遵循这种格式。

    class ClassName 
    {
        /* Class members go here. */
    };
    

    在处理成员函数指针时,vector的value_type应该是成员函数类型。在您的情况下,成员函数类型是 void (detector::*)(int)

    typedef void (detector::*FuncT)(int);
    
    class img_preprocess { 
    public:
       std::vector<FuncT> receivers;
    }
    

    并且在添加成员函数指针时,不能使用对象实例将其添加到向量中。您必须使用:: 运算符。

    this->img_actors[1]->receivers.push_back(&(detector::push));
    

    现在调用时,

    (this->detect_actors[1]->(*this->img_actors[1]->receivers[/* whatever the index */]))(/* whatever the argument */);
    

    现在将所有内容放在一起,您的最终代码将如下所示。

    class detector; // Forward declaration needed for the typedef and img_preprocess object.
    typedef void(detector::* FuncT)(int);
    
    class img_preprocess {
    public:
        std::vector<FuncT> receivers;
    };
    
    class detector {
    public:
        void push(int val) {
            //some code here
        }
    };
    
    
    class tracker {
    private:
        std::map< int, img_preprocess* > img_actors;
        std::map< int, detector* > detect_actors;
    
    public:
        tracker() {
            this->img_actors.insert({ 1, new img_preprocess() });
            this->detect_actors.insert({ 1, new detector() });
    
            // adding the detector::push function to img_preprocess receivers vector
            this->img_actors[1]->receivers.push_back(&(detector::push));
        }
    };
    

    编辑:如果你想存储其他对象的成员函数指针,除了detactor的,你可以使用std::functionstd::bind给vector添加成员函数指针,

    #include <functional>    // For std::function and std::bind
    
    class img_preprocess {
    public:
        std::vector<std::function<void(int)>> receivers;
    };
    
    ...
    this->img_actors[1]->receivers.push_back(std::bind(&detector::push, this->detect_actors[1], std::placeholders::_1));    // Adding the function pointer to the vector.
    this->img_actors[1]->receivers[0](5);    // When calling the function.
    

    补充:Using generic std::function objects with member functions in one class

    【讨论】:

    • 非常感谢您的描述性解释。但就我而言,我需要在接收器向量中存储几个推送函数。这些推送功能来自不同的类,如检测器(例如:matcher::push、distributor::push)。关于如何实现它的任何想法或建议?
    • 您可以使用 lambda 表达式而不是 std::bind[this](int val}{ detect_actors[1].push(val); }
    • 非常感谢@D-RAJ 你的方法对我有用。
    猜你喜欢
    • 1970-01-01
    • 2020-08-21
    • 2011-04-15
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多