问题
Some time ago, Borland have introduced in their BCB evironment an extension to C++ language. This extension is a __closure keyword. The question is, if it is possible to implement such functionality in plain C++ or C++11? If you are not familiar with __closure keyword, below code provides explanation in comments.
Thanks in advance! Toreno
#include <stdio.h>
// __closure keyword is used here !
typedef void (__closure * MemberCallback)(int x, int y, int z);
class A
{
private:
MemberCallback callback;
public:
A() : callback(NULL)
{
}
void setCallback(MemberCallback newCallback)
{
callback = newCallback;
}
void call(int x, int y, int z)
{
if(callback)
callback(x, y, z);
else
printf("NOT SET(%i, %i, %i)\n", x, y, z);
}
};
class B
{
public:
void func1(int x, int y, int z)
{
printf("FUNC 1(%i, %i, %i)\n", x, y, z);
}
void func2(int x, int y, int z)
{
printf("FUNC 2(%i, %i, %i)\n", x, y, z);
}
};
int main()
{
// A and B classes do not know about each other. There is no possibility
// to for inheritance because B class can implement multiple instances
// of callback function
A a;
B b;
a.call(1, 2, 3); // Prints: NOT SET(1, 2, 3)
a.setCallback(b.func1);
a.call(4, 5, 6); // Prints: FUNC 1(4, 5, 6)
a.setCallback(b.func2);
a.call(7, 8, 9); // Prints: FUNC 2(7, 8, 9)
return 0;
}
回答1:
std::function is exactly what you're looking for. If you want to learn how such mechanism is actually implemented in the library, here's a good series of blog posts on it. Combine that with lambda functions for capturing of local variables.
回答2:
Re-hash of the previous answer with the full code, for others like me that want a quick reference to a common pattern:
#include <functional>
#include <stdio.h>
// __closure replacement
typedef std::function<void(int x, int y, int z)> MemberCallback;
class A
{
public:
void setCallback( MemberCallback newCallback ) {
callback_ = newCallback;
}
void call( int x, int y, int z ) {
if ( callback_ )
callback_( x, y, z );
else
printf( "NOT SET(%i, %i, %i)\n", x, y, z );
}
private:
MemberCallback callback_;
};
class B
{
public:
void func1( int x, int y, int z ) {
printf( "FUNC 1(%i, %i, %i)\n", x, y, z );
}
void func2( int x, int y, int z ) {
printf( "FUNC 2(%i, %i, %i)\n", x, y, z );
}
};
int main( )
{
// A and B classes do not know about each other. There is no possibility
// to for inheritance because B class can implement multiple instances
// of callback function
A a;
B b;
a.call( 1, 2, 3 ); // Prints: NOT SET(1, 2, 3)
a.setCallback( [&b](int x, int y, int z){ b.func1(x, y, z); } );
a.call( 4, 5, 6 ); // Prints: FUNC 1(4, 5, 6)
a.setCallback( [&b](int x, int y, int z){ b.func2(x, y, z); } );
a.call( 7, 8, 9 ); // Prints: FUNC 2(7, 8, 9)
return 0;
}
Output:
NOT SET(1, 2, 3)
FUNC 1(4, 5, 6)
FUNC 2(7, 8, 9)
来源:https://stackoverflow.com/questions/31634887/c-pointer-to-member-function-replacement-for-closure