[英]C++ Pointer to member function of an UNKNOWN CLASS
DISCLAIMER I DO NOT USE BOOST
OR OTHER LIBRARIES 免责声明我不使用BOOST
或其他图书馆
Finally I've learned how PointerToMemberFunction works. 最后我了解了PointerToMemberFunction的工作原理。 This is my example code . 这是我的示例代码 。
#include <iostream>
using namespace std;
class Foo
{
public:
void foo ( )
{
cout << "I'm a foo method\n";
};
};
class Bar
{
public:
void bar ( Foo* fooPtr , void(Foo::*fooFnPtr)() )
{
(fooPtr->*fooFnPtr)();
};
};
int main()
{
Foo* foo = new Foo();
Bar* bar = new Bar();
bar->bar ( foo , &Foo::foo );
return 0;
}
Now, what the problem is. 现在,问题是什么。 Bar::bar
must be modified somehow, because in real project it won't know, what class fooFnPtr
is a pointer to . Bar::bar
必须以某种方式进行修改,因为在实际项目中它不会知道, fooFnPtr
类是指向哪个类 。 In other words Bar::bar
must work with any class, not only with Foo
. 换句话说, Bar::bar
必须适用于任何类,而不仅仅是Foo
。 I won't know, a pointer to an instance of what class is passed to Bar::bar
. 我不会知道,指向传递给Bar::bar
类的实例的指针。
The one thing which can help is that all classes which will work with Bar::bar
are children of one class! 可以帮助的一件事是,所有与Bar::bar
一起工作的课程都是一个班级的孩子!
Is this achievable and how? 这是可以实现的吗? How do i fix my code? 我如何修复我的代码? Thanks in advance! 提前致谢!
You could make bar
a template function: 你可以使bar
成为模板函数:
template<class T>
void bar ( T* fooPtr , void(T::*fooFnPtr)() )
{
(fooPtr->*fooFnPtr)();
}
Of course, if you only want to pass pointers to members that exist in the common base class, you can simply do this: 当然,如果您只想将指针传递给公共基类中存在的成员,您可以这样做:
#include <iostream>
using namespace std;
class Foo
{
public:
virtual void foo ( )
{
cout << "I'm a foo method\n";
};
};
class Derived: public Foo
{
public:
virtual void foo ( )
{
cout << "I'm a Derived method\n";
};
};
class Bar
{
public:
void bar ( Foo* fooPtr , void(Foo::*fooFnPtr)() )
{
(fooPtr->*fooFnPtr)();
}
};
int main()
{
Derived* derived = new Derived();
Bar* bar = new Bar();
bar->bar ( derived , &Foo::foo );
return 0;
}
I guess you are after closures and delegates, or the equivalent in C++. 我想你是在关闭和委托之后,或者在C ++中的等价物。 In short, they are instances that can be called, and that contain both the pointer to your class instance (or the instance itself) and the pointer to the method there. 简而言之,它们是可以调用的实例,它包含指向类实例(或实例本身)的指针以及指向该方法的指针。
Take a look to std::bind. 看看std :: bind。 They normally return cryptic instances of template classes, that are all convertible to std::function<...>, in case you need to use an uniformly-typed argument as parameter of a function or method. 它们通常返回模板类的神秘实例,这些实例都可以转换为std :: function <...>,以防您需要使用统一类型的参数作为函数或方法的参数。
So, your code would look like this: 所以,你的代码看起来像这样:
void Bar::bar(std::function<void()> f)
{
....
}
Foo* foo = new Foo();
Bar* bar = new Bar();
auto f = std::bind( &Foo::foo, foo);
bar->bar ( f );
Of course, if you have decided not to use any library, you might nonetheless take a look at how those libraries have solved the problem before, so that you can reinvent the wheel in the best possible way. 当然,如果您决定不使用任何库,您可能会先看看这些库之前是如何解决问题的,这样您就可以以最佳方式重新发明轮子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.