Pointer to Member Function of derived class

Discussion in 'C++' started by Vulcan, May 24, 2008.

  1. Vulcan

    Vulcan Guest

    <code>
    class Base {
    };

    class Class1 : public Base{
    public:
    void Function1();
    }


    void Tracker(Base b, void (Base::*callback)());

    int main(){
    Class1 o1;
    Tracker(&o1, &Class1::Function1);
    }
    </code>

    The above code doesn't work because Function1 is only declared in the
    derived class. Is there a way around this? I want to be able to have a
    member function of a derived class as callback.
     
    Vulcan, May 24, 2008
    #1
    1. Advertising

  2. Vulcan

    Vulcan Guest

    On May 24, 8:14 pm, Vulcan <> wrote:
    > <code>
    > class Base {
    >
    > };
    >
    > class Class1 : public Base{
    > public:
    > void Function1();
    >
    > }
    >
    > void Tracker(Base b, void (Base::*callback)());
    >
    > int main(){
    > Class1 o1;
    > Tracker(&o1, &Class1::Function1);}
    >
    > </code>
    >
    > The above code doesn't work because Function1 is only declared in the
    > derived class. Is there a way around this? I want to be able to have a
    > member function of a derived class as callback.


    I am looking for a way to provide a function Function1 with an object
    Object1 and a member function of that object as a callback.

    The full story:
    I have several classes representing graphical objects like rectangle,
    line, circle all derived from a common base say BObject. I need to be
    able to modify any selected shape on the screen by dragging with the
    mouse. I want to create one function called Tracker which will handle
    mouse move events and update the shape on the screen by calling a
    function on the object being modified.

    Writing mouse tracking code per shape will take up too much repetitive
    code. I felt it would be better for the tracker to have a callback
    which it can call every time the mouse moves and then call ReDraw().
     
    Vulcan, May 24, 2008
    #2
    1. Advertising

  3. Vulcan

    Daniel Pitts Guest

    Vulcan wrote:
    > <code>
    > class Base {
    > };
    >
    > class Class1 : public Base{
    > public:
    > void Function1();
    > }
    >
    >
    > void Tracker(Base b, void (Base::*callback)());
    >
    > int main(){
    > Class1 o1;
    > Tracker(&o1, &Class1::Function1);
    > }
    > </code>
    >
    > The above code doesn't work because Function1 is only declared in the
    > derived class. Is there a way around this? I want to be able to have a
    > member function of a derived class as callback.
    >
    >

    You might have to go the route of using a template for Tracker.
    template<typename T>
    void Tracker(T &base, void (T::*callback)());

    Alternatively, you might want to have callback be a functor, and omit b
    altogether:

    template<typename callback>
    void Tracker(callback &c) { c(); }

    struct Class1Function1Callback {
    Class1 &o;
    Class1Function1Callback(Class1 &o) : o(o) {}
    void operator() { o.Function1(); }

    }
    int main() {
    Class1 o1;
    Tracker(Class1Fucntion1Callback(o1));
    }
    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, May 26, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fraser Ross
    Replies:
    4
    Views:
    1,066
    Fraser Ross
    Aug 14, 2004
  2. Siemel Naran
    Replies:
    4
    Views:
    815
    Micah Cowan
    Jan 12, 2005
  3. Replies:
    1
    Views:
    405
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    395
    Victor Bazarov
    May 23, 2007
  5. Alex Shen
    Replies:
    5
    Views:
    426
    Alex Shen
    Oct 30, 2010
Loading...

Share This Page