c++ - 如何在同一个类的函数中获取类成员的函数指针?

标签 c++ class function-pointers

我对此感到难过。 我有一个带有函数 DoTheThing1 的类 Foo,它接受一个指向带有 0 个参数的 void 函数的指针并调用该函数。

class Foo {
public:
   Foo () {}

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

我有另一个类 Bar,它有一个 Foo 的实例。 Bar 类也有自己的函数 DoTheThing2,它尝试在其构造中将 DoTheThing2 的指针传递给 Foo 的 DoTheThing1

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

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

   void DoTheThing2 () {
      // Something happens.
   }

};

我得到这个错误 error C2664: 'void Foo::DoTheThing1(void (__cdecl *)(void))': cannot convert argument 1 from 'void (__cdecl Bar::* )(void)' to 'void (__cdecl *)(void) 在函数指针 get 传入的那一行。

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

我不确定如何解决这个问题。似乎需要一些奇怪的类型转换。

编辑

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

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

}

编辑

所以现在我正尝试使用类模板破解它,但我一直被这个错误阻止:Term does not evaluate to a function taking 0 arguments.

我想弄清楚我的函数指针为什么不计算任何值。

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

最佳答案

&Bar::DoTheThing2 是成员函数指针,不是常规函数指针。因此错误。这是使用 lambda 和 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.
   }

};

如果 std::functional 被证明是瓶颈,您可以改用模板函数:

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

};

编辑:

如果要存储指向成员函数的指针,还需要该类的实例以便稍后调用它。以下是您可能如何修复示例:

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

关于c++ - 如何在同一个类的函数中获取类成员的函数指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58345417/

相关文章:

c++ - g++ Double Free 或腐败...但是如何?

java - 如何比较原始类类型?喜欢 Integer 和 Void?

PHP:从子类访问 protected var

c++ - 函数指针未分配到函数指针数组中

c - 如何在 C 中声明一组常量函数指针?

c++枚举类未正确显示

c++ - 找出参数是constexpr

C++ - 需要左值作为赋值的左操作数

c++ - 如何将 COM 方法作为函数参数传递?和 Microsoft 编译器错误 C3867

java - 在 Java 中实现 SHA1withRSA