Callback member functions

Discussion in 'C++' started by kankowski@gmail.com, Jun 23, 2006.

  1. Guest

    Hi, I have two methods A::Conv1 and A::Conv2 that transform coordinates
    in two different ways. A::Draw() should take the pointer to one
    of these methods as the parameter, and call it.

    MSVC++ compiler reports: "term does not evaluate to a function taking
    4 arguments". If the CONV type is not a pointer to a member function
    taking 4 arguments, then what a hell it is? :)
    GCC reports:
    "must use .* or ->* to call pointer-to-member function in conv",
    which doesn't helps, too.

    How should I call the conv function?

    class A {
    public:
    void Conv1(double X, double Y, int& x, int& y);
    void Conv2(double X, double Y, int& x, int& y);
    typedef void (A::*CONV)(double, double, int&, int&);
    void Draw(CONV conv);
    void ReDraw();
    A() {
    _xscale = 0.5, _yscale = 0.5, _xshift = 100, _yshift = 200;
    }
    private:
    double _xscale, _yscale;
    int _xshift, _yshift;
    };

    void A::Conv1(double X, double Y, int& x, int& y) {
    x = (int)(X * _xscale), y = (int)(Y * _yscale);
    }
    void A::Conv2(double X, double Y, int& x, int& y) {
    x = (int)(X * _xscale) + _xshift, y = (int)(Y * _yscale) + _yshift;
    }
    void A::Draw(CONV conv) {
    int x, y;
    conv(0.5, 0.5, x, y); // error C2064: term does not evaluate to a
    function
    }

    void A::ReDraw() {
    Draw(Conv1);
    }

    int main() {
    A a;
    a.ReDraw();
    return 0;
    }
     
    , Jun 23, 2006
    #1
    1. Advertising

  2. wrote:
    > [..]
    > void A::Draw(CONV conv) {
    > int x, y;
    > conv(0.5, 0.5, x, y); // error C2064: term does not evaluate to a
    > function


    The syntax to call a member function through a pointer to member is

    (this->*conv)(0.5, 0.5, x, y);

    What book are you reading that doesn't explain that?

    > }
    >
    > void A::ReDraw() {
    > Draw(Conv1);
    > }
    >
    > int main() {
    > A a;
    > a.ReDraw();
    > return 0;
    > }


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 23, 2006
    #2
    1. Advertising

  3. Guest

    I've come up with workaround using friend functions. It does exactly
    what I want, but the syntax looks terrible. If someone knows how
    to solve the initial problem, I will be very glad to hear you.

    #include <stdio.h>
    class A;
    typedef void (*CONV)(A*, double, double, int&, int&);

    class A {
    public:
    void Draw(CONV conv);
    void ReDraw();
    A() {
    _xscale = 5.0, _yscale = 5.0, _xshift = 100, _yshift = 200;
    }
    friend void Conv1(A* a, double X, double Y, int& x, int& y);
    friend void Conv2(A* a, double X, double Y, int& x, int& y);
    private:
    double _xscale, _yscale;
    int _xshift, _yshift;
    };

    void Conv1(A* a, double X, double Y, int& x, int& y) {
    x = (int)(X * a->_xscale),
    y = (int)(Y * a->_yscale);
    }
    void Conv2(A* a, double X, double Y, int& x, int& y) {
    x = (int)(X * a->_xscale) + a->_xshift,
    y = (int)(Y * a->_yscale) + a->_yshift;
    }
    void A::Draw(CONV conv) {
    int x, y;
    conv(this, 0.5, 0.5, x, y); // error C2064: term does not evaluate to
    a function
    printf("x=%d y=%d\n", x, y);
    }

    void A::ReDraw() {
    printf("Conv1:\n");
    Draw(Conv1);
    printf("Conv2:\n");
    Draw(Conv2);
    }

    int main() {
    A a;
    a.ReDraw();
    return 0;
    }
     
    , Jun 23, 2006
    #3
  4. * :
    > Hi, I have two methods A::Conv1 and A::Conv2 that transform coordinates
    > in two different ways. A::Draw() should take the pointer to one
    > of these methods as the parameter, and call it.
    >
    > MSVC++ compiler reports: "term does not evaluate to a function taking
    > 4 arguments". If the CONV type is not a pointer to a member function
    > taking 4 arguments, then what a hell it is? :)
    > GCC reports:
    > "must use .* or ->* to call pointer-to-member function in conv",
    > which doesn't helps, too.


    Why doesn't it help?

    It tells you exactly what's /technically/ wrong, and what you must do to
    fix it /technically/.


    > How should I call the conv function?


    See the GCC error message.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jun 23, 2006
    #4
  5. Phlip Guest

    Phlip, Jun 23, 2006
    #5
  6. Guest

    Thank you, it works now. Many thanks!
     
    , Jun 23, 2006
    #6
    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. cadull

    Member Callback Functions

    cadull, May 6, 2005, in forum: C++
    Replies:
    1
    Views:
    500
    cadull
    May 6, 2005
  2. Hamish
    Replies:
    3
    Views:
    607
    Alf P. Steinbach
    Jan 25, 2008
  3. flounder
    Replies:
    4
    Views:
    532
    flounder
    Apr 2, 2009
  4. Hicham Mouline
    Replies:
    0
    Views:
    456
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    444
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page