function与非静态成员函数

function与非静态成员函数

本文介绍了std :: function与非静态成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解一个概念和一个错误。这是怎么了?

i'm trying to understand a concept and an error. what's wrong with this?

class A
{
public:
    A()
    {
        std::function<void(int)> testFunc(&A::func);
    }

private:
    void func(int) {}
}

我的问题是,是否有可能创建能够调用特定实例成员的任何类型的对象,其中std :: function的行为类似于成员函数指针,除非没有古怪的类型不能在继承类中用作函数参数的定义。例如:

my question, is it possible to create any sort of object that is able to call a member of a specific instance where std::function acts like a member function pointer, except without the wacky type definition that can't be used as function parameters in inheriting classes. for example:

class A
{
public:
    A()
    {
         index[WM_CREATE] = &A::close;
         index[WM_DESTROY] = &A::destroy;
    }

protected:
    map<UINT msg, void (A::*)(HWND, UINT , WPARAM, LPARAM)> index;
    void close(HWND,UINT, WPARAM, LPARAM);
    void destroy(HWND, UINT, WPARAM, LPARAM);
};

class B : public A
{
public:
    B()
    {
        index[WM_CREATE] = &B::create; // error because it's not a pointer of type A::*
    }
private:
    void create(HWND, UINT, WPARAM, LPARAM);

};

我认为我在使用std :: function之类的正确方法上:

I'm thinking i'm on the right track of using std::functions like so:

class A
{
public:             //         Gigantic stl error with these two
    A()             //                         |
    {               //                         V
         index[WM_CREATE] = std::function<void(HWND, UINT, WPARAM, LPARAM>(&A::close);
         index[WM_DESTROY] = std::function<void(HWND, UINT, WPARAM, LPARAM>(&A::destroy);
    }

protected:
    map<UINT msg, std::function<void(HWND, UINT, WPARAM, LPARAM)> > index;
    void close(HWND,UINT, WPARAM, LPARAM);
    void destroy(HWND, UINT, WPARAM, LPARAM);
};

class B : public A
{
public:                    //               and this one
    B()                    //                     |
    {                      //                     V
        index[WM_CREATE] = std::function<void(HWND, UINT, WPARAM, LPARAM)>(&B::create);
    }
private:
    void create(HWND, UINT, WPARAM, LPARAM);

};

如果有人可以解释这些巨大的隐秘错误的含义,那么w修复它们,我将不胜感激。

if someone could explain what these giant cryptic errors mean and how to fix them, i would greatly appreciate it.

推荐答案

我认为您遇到的问题是成员函数不需要只是函数指针,而是指向调用对象的指针。换句话说,成员函数还有一个额外的隐式参数,它是指向调用对象的指针。

I think the problem you are having is that a member function requires not only a function pointer, but a pointer to the calling object. In other words, member functions have an additional implicit argument that is the pointer to the calling object.

要将成员函数设置为std :: function,您需要使用std :: bind这样:

To set a member function to a std::function, you need to use std::bind like this:

std::function<void(int)> testFunc(std::bind(&A::func, this, _1));

此操作将当前A实例的this指针绑定到函数,因此具有函数指针和对象实例,该信息足以正确调用该函数。 _1参数表示在调用函数时将提供第一个显式参数。

This binds the this pointer of the current A instance to the function so it has the function pointer and the object instance, which is enough information to properly call the function. The _1 argument indicates that the first explicit argument will be provided when the function is called.

这篇关于std :: function与非静态成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:41