How to call pointer to member function, which has been saved in a vector of custom struct? [closed]

点点圈 提交于 2019-12-08 14:08:47

问题


My question is actually regarding already asked question. I have tried the answer given by @r3mus n0x also have seen some SO questions which did not help me to get a clear idea about the above situation.

In the given post lacks MCVE, therefore I have tried a bit and came up with the following code and with the same error what @user10213044 mentioned in his/her post.

Error msg

error C2065: 'm_func': undeclared identifier

My qestion:

Q1: Can we really store the pointer to some of the member functions of a class(like in the following example) into it's on private member(ex. vector array)? If so what is the reason for the above error msg?

Q2: I have also tried to write inside the for loop:

classFuncPtr fun = bindClassPtr->m_func; // compiles
fun(str); // error

gave me: Error msg

error: must use '.*' or '->*' to call pointer-to-member function in 'fun (...)', e.g. '(... ->* fun) (...)'
 fun(str); // error

which I could not understand. Can anybody tell me what went wrong in this case?

The second attempt was similar to the following case which we use for normal functions pointer case.

typedef void(*FuncPtr)(const std::string&);
FuncPtr Lambda = [](const std::string& str) { std::cout << str << std::endl; };
Lambda(std::string("Hellow World"));

Here is the code I tried:

#include <iostream>
#include <vector>
#include <string>
#include <memory>

class MyClass;          
typedef void (MyClass::*classFuncPtr)(const std::string&); // function ptr to MyClass::member functions

struct MyBind // bind struct
{
   classFuncPtr m_func;
   explicit MyBind(const classFuncPtr& func): m_func(std::move(func)) {}
};

class MyClass
{
    std::string m_var;
    std::vector<std::unique_ptr<MyBind>> my_binds_;
public:
   MyClass() // constructor 
   {
      my_binds_.emplace_back( std::make_unique<MyBind>( std::move(&MyClass::do_this) ));
      my_binds_.emplace_back( std::make_unique<MyBind>( std::move(&MyClass::do_that) ));
   }

   // two functions to bind
   void  do_this (const std::string& str) { std::cout << "From do this: " << str << std::endl;   }
   void  do_that (const std::string& str) { std::cout << "From do that: " << str << std::endl;   };

   void handle_input(const std::string& str)
   {

      for (const std::unique_ptr<MyBind>& bindClassPtr: my_binds_)
      {
          // how to print passed string str here??              (Q1)
          (bindClassPtr->*m_func)(str);

          /*
          classFuncPtr fun = bindClassPtr->m_func; // compiles   (Q2)
          fun(str); // error
          */
      }
   }
};

回答1:


Your first attempt fails because there's no variable in scope named m_func.

Your second attempt fails because a pointer-to-member requires an object to be called on.

The correct syntax is:

classFuncPtr fun = bindClassPtr->m_func;
(this->*fun)(str);

Live Demo

The pointer contained in your MyBind objects isn't actually bound to anything. It's a pointer to a member of MyClass, so you have to provide it an instance of MyClass to work on.



来源:https://stackoverflow.com/questions/51904472/how-to-call-pointer-to-member-function-which-has-been-saved-in-a-vector-of-cust

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!