• Advertisement
Sign in to follow this  

C++ Member function pointers wrapper

This topic is 3653 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings, I'm using a GUI program to help me expand upon my C++ knowledge when I came across the issue of linking a Button to some function. I wanted to be able to link a wide variety of functions to any Button on the fly and came up with this. My question is, are there any pitfalls from this method?
// Function Pointer wrapper
class FunctionPointer
{
public:
    virtual void Call() {}
};

template <typename T>
class FunctionPointerTemplate : public FunctionPointer
{
public:
    FunctionPointerTemplate(T* object, void (T::*member_function)())
    { pObject = object; pMemberFunction = member_function; }

    void Call()
    { (pObject->*(pMemberFunction))(); }

private:
    T* pObject;
    void (T::*pMemberFunction)(); 
};
// Button class member function, called upon to link a function to the button action.
    template <typename T>
    void LinkFunction(T* object, void (T::*member_function)())
    {
        if (m_pFunction != NULL) // m_pFunction is initialized to NULL in constructor
        { delete m_pFunction; }

        FunctionPointerTemplate<T>* function = new FunctionPointerTemplate<T>(object, member_function);
        m_pFunction = function;
    }
// Linking a function (the 'Apply' function of a dialog window) to the Button
pButton->LinkFunction(pDialogWindow, &DialogWindow::Apply);
This currently limits me to only functions that return nothing and take no parameters, but that works for now so I'll address that at a later time. Comments?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
My question is, are there any pitfalls from this method?


Yes, there can be, if functions are connected this way between different compilation units. It's an obscure problem, but it can happen.

Also, unless you have specific requirements with regard to allocation or thread-safety, there's boost::function and boost::bind, which already provide all possible bindings for almost arbitrary number of parameters.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Quote:
My question is, are there any pitfalls from this method?


Yes, there can be, if functions are connected this way between different compilation units. It's an obscure problem, but it can happen.

Also, unless you have specific requirements with regard to allocation or thread-safety, there's boost::function and boost::bind, which already provide all possible bindings for almost arbitrary number of parameters.


Could you expand upon the obscure problem? I'm not very familiar with how the different compilation units are defined, etc.

I'm probably going to switch over to using boost, but for now I'm using this as a learning excercise. Thanks!

Share this post


Link to post
Share on other sites
Hi Mantear,

Forgive me using this way to find you. I saw you posted a thread in 3/13/2005 regarding "VBO and picking". I am also having this problem now. Did you find the solution for it? I will appreciate it if you can share the solution. Please email me at myfeng@hotmail.com if possible.

Thanks a lot,
myfeng

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement