How to get the function pointer of a class member in a function of the same class?

北城余情 提交于 2019-12-23 04:58:28

问题


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();
   }
};

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.
   }

};

I get this error error C2664: 'void Foo::DoTheThing1(void (__cdecl *)(void))': cannot convert argument 1 from 'void (__cdecl Bar::* )(void)' to 'void (__cdecl *)(void) at the line where the function pointer get's passed in.

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

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

EDIT

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

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();
}

回答1:


Old way: You need a template to get the class and a specialization for functions.

Working example:

#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();
}



回答2:


&Bar::DoTheThing2 is a member function pointer, not a regular function pointer. Hence the error. Here's a workaround with lambdas and std::functional:

#include <functional>

class Foo {
public:
   Foo () {}

   void DoTheThing1 (std::function<void()>& theThing) {
      theThing();
   }
};

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

   Bar () {
      foo->DoTheThing1([this](){ DoTheThing2(); });
   }

   void DoTheThing2 () {
      // Something happens.
   }

};

If std::functional proves to be a bottleneck, you can use a templated function instead:

#include <functional>

class Foo {
public:
   Foo () {}

   template <typename Callable> 
   void DoTheThing1 (Callable&& theThing) {
      theThing();
   }
};

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

   Bar () {
      foo->DoTheThing1([this](){ DoTheThing2(); });
   }

   void DoTheThing2 () {
      // Something happens.
   }

};

Edit:

If you want to store a pointer to a member function, you also need an instance of that class in order to be able to call it later. Here's how you might fix your example:

#include <iostream>

template <typename U>
class Foo {
public:
   void (U::*m_theThing) ();
   U* m_u;

   Foo (U* u): m_u{u} {}

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

   void DoTheThing1 () {
      (m_u->*m_theThing)(); // Works fine.
   }

};

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

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

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


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


来源:https://stackoverflow.com/questions/58345417/how-to-get-the-function-pointer-of-a-class-member-in-a-function-of-the-same-clas

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