如何调用指向成员函数的指针,该成员函数已保存在自定义结构的向量中? [英] How to call pointer to member function, which has been saved in a vector of custom struct?

查看:183
本文介绍了如何调用指向成员函数的指针,该成员函数已保存在自定义结构的向量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题实际上是关于已经问到的问题。我已经尝试过给出给出的答案@ r3mus n0x的作者也看到了一些SO问题,这些问题并没有帮助我对上述情况有一个清晰的认识。



在给定的帖子中缺少MCVE ,因此我尝试了一下,并想出了以下代码,并且出现了@ user10213044在他/她的帖子中提到的相同错误。



错误消息

 错误C2065: m_func:未声明的标识符

我的问题:



第一季度:我们真的可以存储指向类的某些成员函数(如以下示例)的指针是否在私有成员(例如向量数组)上?如果是这样,则出现上述错误消息的原因是什么?



第二季度:我也尝试过在for循环内编写:

  classFuncPtr fun = bindClassPtr-> m_func; //编译
fun(str); //错误

给我: 错误消息

 错误:必须使用。*或-> *来调用 fun(...)中的指针成员函数,例如’(...-> *有趣)(...)’
fun(str); //错误

我无法理解。有人能告诉我在这种情况下出了什么问题吗?



第二次尝试类似于以下用于正常函数指针情况的情况。

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

这是我尝试的代码:

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

class MyClass;
typedef void(MyClass :: * classFuncPtr)(const std :: string&); //将函数ptr转换为MyClass ::成员函数

struct MyBind //绑定结构
{
classFuncPtr m_func;
显式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()//构造函数
{
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))));
}

//绑定
的两个函数void do_this(const std :: string& str){std :: cout<< 从此开始:<< str<< std :: endl; }
void do_that(const std :: string& str){std :: cout<< 从那开始:<< str<< std :: endl; };

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

for(const std :: unique_ptr< MyBind>& bindClassPtr:my_binds_)
{
//如何在此处打印传递的字符串str? (Q1)
(bindClassPtr-> * m_func)(str);

/ *
classFuncPtr fun = bindClassPtr-> m_func; //编译(Q2)
fun(str); //错误
* /
}
}
};


解决方案

您的第一次尝试失败了,因为范围内没有变量 m_func



您的第二次尝试失败,因为指向成员的指针要求调用对象。 / p>

正确的语法是:

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

实时演示



MyBind 对象中包含的指针是'实际上绑定任何东西。它是指向 MyClass 成员的指针,因此您必须为其提供 MyClass 的实例才能使用。 / p>

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
          */
      }
   }
};

解决方案

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.

这篇关于如何调用指向成员函数的指针,该成员函数已保存在自定义结构的向量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆