首页 文章

在静态类函数成员中获取类对象指针

提问于
浏览
-1

有任何“智能”方法从静态类函数成员获取类对象指针,例如:

class my_class
{
public:

    my_class(int a) : v_a(a) {};

    static void static_f()
    {
        my_class* p = GET OBJECT POINTER
        std::cout << p->v_a << std::endl;
    }

private:
    int v_a;
};

我知道这可能是不可能的,但我已经奋斗了2天到目前为止,我已经能够使用模板实现它并在全局范围内声明对象:

template<my_class* tp>
    static void static_f()
    {
        my_class* p = tp;
        std::cout << p->v_a << std::endl;
    }

  my_class v1(100); // global variable
  my_class v2(200); // global variable

  v1.static_f<&v1>(); // main function
  v2.static_f<&v2>(); // main function

它打印出100和200尊重 . 如果我将它声明为局部变量,这将无法工作 . 通过“智能”我的意思是c 11或更高的一些新功能,我不知道 .

Purpose:

我试图封装一个C库的几个函数,其中一个函数需要一个指向函数的指针作为回调,这个回调函数指针不会由我自己调用,而是由库调用,它不接受传递的其他参数作为上下文,所以我不能直接调用它(我在代码中做了,因为我想保持示例尽可能小)或只是传递对象指针 .

Possible solution:

到目前为止,我已经能够通过使用模板和每个对象的全局变量来实现这一目标:

template<void** pp>
class my_class
{
public:

    my_class(int v1, int v2)
    {
        x_var1 = v1;
        x_var2 = v2;
        *pp = this;
    }

    void call_callback_for_test()
    {
        callback();
    }

private:

    static void callback()
    {
        my_class* p = reinterpret_cast<my_class*>(*pp);
        std::cout << p->x_var1 << " - " << p->x_var2 << std::endl;
    }


    int x_var1;
    int x_var2;
};


void* pv1;
void* pv2;
my_class<&pv1> v1(100, 200);


int main()
{
    my_class<&pv2> v2(300, 400);

    v1.call_callback_for_test();
    v2.call_callback_for_test();
    return 0;
}

是的,我觉得可以有一个更好的解决方案使用更新的c功能,例如使用constexpr或元编程(我读了一下他们,我可能是错的)但我只是非常初学者和缺乏经验,或改进这种方法 .

2 回答

  • 1

    做你要求的事情并不难;它可能看起来像这样:

    class Foo {
    private:
        int bar_;
        static Foo* pFoo;
    
    public:
        explicit Foo( int a ) : bar_( a ) {
            pFoo = this;
        }
    
        static void printBar() {
            std::cout << pFoo->bar_ << '\n';
        }
    };
    
    Foo* Foo::pFoo = nullptr;
    
    int main() {
        Foo f( 3 );
        f1.printBar();
    
        return 0;
    }
    

    -Output-

    3
    

    这可以工作,但你必须谨慎静态指针的工作方式 . 以此程序为例:

    int main() {
        Foo f1( 3 );
        f1.printBar();
    
        Foo f2( 8 );
        f2.printBar();
    
        return 0;
    }
    

    -Output-

    3
    8
    

    好的,我们有2个 Foo 个实例; f1f2 并分别输出 38 ;那为什么我们需要谨慎?让我们再次运行上面的内容,但添加了一行编码 .

    int main() {
        Foo f1( 3 );
        f1.printBar();
    
        Foo f2( 8 );
        f2.printBar();
    
        // call f1's printBar() again
        f1.printBar(); // What do you think the output will be? 
        // I will tell you that it is not 3!
    
        return 0;
    }
    

    -Output-

    3
    8
    8
    

    这可能是你正在寻找的,但你必须是一个类的成员静态指针的良心,以及它们如何与一个类的多个实例行为 .

    在上面的示例中, f1 的成员 bar_ 在其构造函数中设置为 3 ,然后将静态指针设置为this . 然后当我们创建 f2 并使用值 8 调用它的构造函数时;它将 f2bar_ 设置为 8 ,然后再次设置 static pointer . 由于 static storage ,这也恰好将 f1bar3 更改为 8 . 这是你需要注意的事情!


    即使您在初始化值之后通过类的范围解析运算符调用静态方法,它仍将产生相同的结果 .

    int main() {
        Foo f1( 3 );
        f1.printBar();
    
        Foo f2( 8 );
        f2.printBar();
    
        f1.printBar(); 
    
        Foo::printBar();
    
        return 0;
    }
    

    -Output-

    3
    8
    8
    8
    

    这只是类的成员静态指针如何工作的一般概念,以及静态方法 .


    用户Alan Birtles提到了我在写这个答案时没有想到的警告或陷阱 . 他说:

    请注意,如果最近创建的对象已被销毁,则回调将具有未定义的行为 .

  • 0

    一种选择是使用标记结构作为模板参数,为每个回调生成不同的类型:

    #include <iostream>
    #include <string>
    #include <memory>
    
    template < typename T >
    class Foo
    {
    public:
        static void test()
        {
            std::cout << instance() << "\n";        
        }
    
        static std::shared_ptr< Foo< T > > instance()
        {
            static std::shared_ptr< Foo< T > > foo( new Foo< T >() );
            return foo;
        }
    
    private:
        Foo() {}
    };
    
    struct instance1 {};
    struct instance2 {};
    
    typedef void(*callback_t)();
    
    int main()
    {
        callback_t callback1 = Foo< instance1 >::test;
        callback_t callback2 = Foo< instance2 >::test;
        callback1();
        callback2();
    }
    

    我已经使用了共享指针来确保在执行回调时仍然存在创建的对象(注意这些将在程序的生命周期中存在) . 如果你想自己控制生命周期,你可以使用弱指针代替:

    #include <iostream>
    #include <string>
    #include <memory>
    
    template < typename T >
    class Foo
    {
    public:
        static void test()
        {
            std::cout << instance() << "\n";        
        }
    
        static std::shared_ptr< Foo< T > > instance()
        {
            static std::weak_ptr< Foo< T > > foo;
            std::shared_ptr< Foo< T > > result = foo.lock();
            if ( !result )
            {
                // will need a mutex here in multi-threaded applications
                result.reset( new Foo< T >() );
                foo = result;
            }
            return result;
        }
    
    private:
        Foo() {}
    };
    
    struct instance1 {};
    struct instance2 {};
    
    typedef void(*callback_t)();
    
    int main()
    {
        {
            auto foo = Foo< instance1 >::instance();
            callback_t callback1 = decltype(foo)::element_type::test;
            callback1();
        }
        {
            auto foo = Foo< instance2 >::instance();
            callback_t callback2 = decltype(foo)::element_type::test;
            callback2();
        }
        {
            auto foo = Foo< instance1 >::instance();
            // using a different object to callback1
            callback_t callback3 = decltype(foo)::element_type::test;
            callback3();
        }
    }
    

    如果在没有活动对象的情况下调用回调,则将创建并销毁新对象,而不是使程序崩溃 .

相关问题