C++:将指向一个对象的成员函数的指针存储在另一个对象中

C++: Store pointer to a member function of an object in another object

本文关键字:一个对象 存储 指针 函数 成员 C++      更新时间:2023-10-16

我有一个类,它在某些情况下会调用用户指定的函数。因此,该类具有可用于"注册"函数的方法void setExternalPostPaintFunction(void(*function)(QPainter&));。然后,将在那个场合调用此函数:

class A {
    public:
        void setExternalPostPaintFunction(void(*function)(QPainter&));
    private:
        void (*_externalPostPaint)(QPainter&);
        bool _externalPostPaintFunctionAssigned;
};

函数指针保存在成员变量 _externalPostPaint 中。setExternalPostPaintFunction的实现如下所示:

void A::setExternalPostPaintFunction(void(*function)(QPainter&)) {
    _externalPostPaint = function;
    _externalPostPaintFunctionAssigned = true;
}

现在,这适用于正常功能。但是,我希望还能够传递指向对象成员函数的指针。据我所知,在这种情况下,我还必须传递并存储指向对象的指针。但是,我不知道另一个对象将具有哪种类型。所以我想我被迫使用模板。我已经想到了这样的事情:

class A {
    public:
        template <typename T>
        void setExternalPostPaintFunction(void(T::*function)(QPainter&), T* object);
    private:
        void (T::*_externalPostPaint)(QPainter&);       //<- This can't work!
        bool _externalPostPaintFunctionAssigned;
};

这样,我可以将函数指针和对象指针传递给setExternalPostPaintFunction并且可能能够在该函数内的对象上调用该函数。但是我无法将其存储在变量_externalPostPaint中,因为只有在调用函数setExternalPostPaintFunction时才会推断出T类型,因此我不能拥有依赖于此类型的成员变量,因为我的成员变量的类型必须在创建对象时知道,除此之外它不能更改, 但是,在分配一个新函数的情况下,它必须是不同类型的对象的成员函数。

那么正确的方法是什么,或者有没有?我不太适合模板和函数指针,所以我可能忽略了一些东西。

Anoter 选项肯定是创建一个具有虚拟成员函数的函子类,该函数可以在派生类中覆盖,然后传递 + 存储该类型的对象指针而不是函数指针。但如果可能的话,我不知何故更喜欢我的方法。

编辑:解决方案

TartanLlama通过建议使用std::function使我走上了正确的轨道。这是我解决它的方法:

class A {
    public:
        template <typename T>
        void setExternalPostPaintFunction(T* object, void(T::*function)(QPainter&)) {
            _externalPostPaint = std::bind(function, object, std::placeholders::_1);
            _externalPostPaintFunctionAssigned = true;
        }
        void setExternalPostPaintFunction(std::function<void(QPainter&)> const& function);
    private:
        std::function<void(QPainter&)> _externalPostPaint;
        bool _externalPostPaintFunctionAssigned;
};

如您所见,指向函数/成员函数的指针现在存储在 std::function<void(QPainter&)> 对象中。优点是,std::function基本上可以存储任何可调用的目标。然后有两个重载:一个可用于任何也接受例如普通函数指针的std::function对象(因为预期的std::function是从该指针隐式构造的),另一个用于必须在对象上调用的成员函数(更多是为了方便)。后者作为模板实现。这使用 std::bind 在对象(用户传递)上创建该成员函数(用户传递)调用的std::function对象。

采用std::function的重载在源文件中实现,如下所示:

void ImageView::setExternalPostPaintFunction(std::function<void(QPainter&)> const& function) {
    _externalPostPaint = function;
    _externalPostPaintFunctionAssigned = true;
}

A 的代码中调用该存储函数现在非常简单:

//canvas is a QPainter instance    
if (_externalPostPaintFunctionAssigned) _externalPostPaint(canvas);

想要将成员函数注册为回调函数的用户只需执行以下操作:

//_imageView is an instance of "A"
//"MainInterface" is the type of "this"
_imageView->setExternalPostPaintFunction(this, &MainInterface::infoPaintFunction);

或者,如果它不是成员函数,而只是一个普通函数:

void someFunction(QPainter& painter) {
    //do stuff
}
_imageView->setExternalPostPaintFunction(&someFunction);

或者他可以显式创建一个std::function对象并传递它:

std::function<void(QPainter&)> function = [&](QPainter& painter){ this->infoPaintFunction(painter); };
_imageView->setExternalPostPaintFunction(function);

就像一个魅力。

您可以使用

std::function

class A {
    public:
        //PostPaintFun can be anything which acts as a function taking a QPainter&
        //Could be a lambda, function pointer, functor, etc.
        using PostPaintFun = std::function<void(QPainter&)>;
        void setExternalPostPaintFunction(PostPaintFun fun);
    private:
        //Names beginning with an underscore are reserved, don't use them
        //Ending with an underscore is fine
        PostPaintFun fun_;
        bool externalPostPaintFunctionAssigned_;
};

现在,您可以使用如下成员函数:

struct B
{
    void exec(QPainter&) const;
};
void foo() {
    B b;
    a.setExternalPostPaintFunction(
        [b] (QPainter& p) {b.exec(p);}
    );
}
//or inside B
void B::foo() {
    a.setExternalPostPaintFunction(
        [this] (QPainter&p) {this->exec(p);}
    );
 }

我不得不说我更喜欢TartanLlama的答案,但在这里你有一些可以为你工作的东西。

这可能需要一些工作,但我相信你会明白的。

struct IFunctionHolder {};                      // Used for pointing to any FunctionHolder
typedef IFunctionHolder* functionHolder_ptr;    // Alias for IFunctionHolder* .
template<typename Function>                     // The template for the actual function holders.
struct FunctionHolder:  public IFunctionHolder
{
    Function function;
};

class A {
    public:
        template <typename T>
        void setExternalPostPaintFunction(void(T::*function)(QPainter&), T* object);
    private:
        functionHolder_ptr *function_holder;            // This memeber can hold eny instantiation of template<> FunctionHolder.
                                                        // Instantiate this member wen calling setExternalPostPaintFunction
        bool _externalPostPaintFunctionAssigned;
};

你可以有一些这样的代码:

A some_a;
void some_a.setExternalPostPaintFunction(&SomeInstance::some_fnunction);    // Here take place the instantiation of FunctionHolder.
some_a.function_holder.function(some_painter);
相关文章: