C++ Member function pointers wrapper

Started by
2 comments, last by myfeng 16 years, 1 month ago
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?
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.
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!
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

This topic is closed to new replies.

Advertisement