我对此感到难过。
我有一个带有函数 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/