如何在同一类的函数中获取类成员的函数指针? [英] How to get the function pointer of a class member in a function of the same class?

查看:61
本文介绍了如何在同一类的函数中获取类成员的函数指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此感到困惑。
我有一个类 Foo ,该类具有函数 DoTheThing1 ,该函数需要一个指向具有0个参数的void函数的指针

I'm stumped on this. I have a class Foo with a function DoTheThing1 that takes a pointer to a void function with 0 parameters and calls the function.

class Foo {
public:
   Foo () {}

   void DoTheThing1 (void (*theThing)()) {
      theThing();
   }
};

我还有另一个类 Bar Foo 的实例。
Bar 还具有自己的函数 DoTheThing2 ,它尝试在其构造中传递<指针code> DoTheThing2 到 Foo's DoTheThing1

I have another class Bar that has an instance of Foo. Class Bar also has its own function DoTheThing2 where it tries in it's construct to pass a pointer of DoTheThing2 to Foo's DoTheThing1.

class Bar {
public:
   Foo* foo = new Foo();

   Bar () {
      foo->DoTheThing1(&Bar::DoTheThing2);
   }

   void DoTheThing2 () {
      // Something happens.
   }

};

我收到此错误错误C2664:'void Foo :: DoTheThing1(void (__cdecl *)(void))':无法将参数1从'void(__cdecl Bar :: *)(void)'转换为'void(__cdecl *)(void)

Bar () {
      foo->DoTheThing1(&Bar::DoTheThing2); /// Does not like.
   }

我不确定如何解决此问题。

I'm unsure of how to work around this. It seems some weird cast is required.

编辑

实际上,我的情况比仅仅复杂一些从自身内部的类成员调用函数指针。我的代码实际执行的操作是将指针设置为变量,然后稍后调用它。

Actually, my situation is a little more complicated than just calling a function pointer from a class member within itself. What my code actually does is set the pointer to a variable, then it gets called later.

class Foo {
public:

   void (*m_onEvent) ();

   Foo () {}

   void SetTheThing (void (*theThing)()) {
      m_onEvent = theThing;
   }

   template <typename T>
   void SetTheThing (T&& theThing) {
      m_onEvent = theThing;
   }

   void DoTheThing1 () {
      m_onEvent();
   }
};

class Bar {
public:
   Foo* foo = new Foo();

   Bar () {
      foo->SetTheThing([this](){ DoTheThing2(); });  // error C2440:  '=': cannot convert from 'T' to 'void (__cdecl *)(void)'
      foo->SetTheThing(&DoTheThing2);                // '&' illegal operation on bound member function expression.
   }

   void DoTheThing2 () {
      std::cout << "I did the thing." << std::endl;
   }

};


int main () {

   Bar* bar = new Bar();
   bar->foo->DoTheThing1();

}

EDIT

因此,现在我正尝试使用类模板对其进行破解,但我一直被该错误所阻止: Term的求值结果不是带有0个参数的函数。

So now I'm trying to hack it using a class template, but I keep getting stopped by this error: Term does not evaluate to a function taking 0 arguments.

我正在尝试弄清楚我的函数指针如何对任何东西都不求值。

I'm trying to figureout how my function pointer doesn't evaluate to anything.

template <typename U>
class Foo {
public:


   void (U::*m_theThing) ();

   Foo () {}

   void SetTheThing (void (U::*theThing)()) {
      m_theThing = theThing;
   }

   void DoTheThing1 () {
      m_theThing(); // Term does not evaluate to a function taking 0 arguments.
   }
};

class Bar {
public:
   Foo<Bar>* foo = new Foo<Bar>();

   Bar () {
      foo->SetTheThing(&Bar::DoTheThing2);
   }

   void DoTheThing2 () {
      std::cout << "I did the thing." << std::endl;
   }
};


int main () {
   Bar* bar = new Bar();
   bar->foo->DoTheThing1();
}


推荐答案

旧方法:您需要

工作示例:

#include <iostream>

//For member function of class C
template <typename C = void>
struct TheCaller
{
    TheCaller() : theClass(nullptr), mf(nullptr) {}
    C* theClass;
    void (C::*mf)();

    void SetTheThing(C* aClass, void (C::*memberFunction)())
    {
        theClass = aClass;
        mf = memberFunction;
    }

    void CallTheThing()
    {
        if ( theClass )
            (theClass->*mf)();
    }
};

//Specialization for any function
template <>
struct TheCaller<void>
{
    TheCaller() : mf(nullptr) {}
    void (*mf)();

    void SetTheThing(void (*memberFunction)())
    {
        mf = memberFunction;
    }

    void CallTheThing()
    {
        if ( mf )
            mf();
    }
};

struct Bar
{
    void DoTheBarThing()
    { std::cout << "DoTheBarThing called" << std::endl; }
};

void AFunction()
{ std::cout << "AFunction called" << std::endl; }

int main()
{
    TheCaller<Bar> foo;
    Bar bar;
    foo.SetTheThing(&bar, &Bar::DoTheBarThing);
    foo.CallTheThing();

    TheCaller<> foo2;
    foo2.SetTheThing(&AFunction);
    foo2.CallTheThing();
}

这篇关于如何在同一类的函数中获取类成员的函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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