简体   繁体   English

C++ 指向成员函数的指针,替换 __closure

[英]c++ pointer to member function, replacement for __closure

Some time ago, Borland have introduced in their BCB evironment an extension to C++ language.前段时间,Borland 在他们的 BCB 环境中引入了对 C++ 语言的扩展。 This extension is a __closure keyword.这个扩展是一个 __closure 关键字。 The question is, if it is possible to implement such functionality in plain C++ or C++11?问题是,是否可以在普通的 C++ 或 C++11 中实现这样的功能? If you are not familiar with __closure keyword, below code provides explanation in comments.如果您不熟悉 __closure 关键字,下面的代码在注释中提供了解释。

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

std::function is exactly what you're looking for.std::function正是您要寻找的。 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.将其与用于捕获局部变量的lambda 函数结合使用。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM