Callback member functions

K

kankowski

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;
}
 
V

Victor Bazarov

[..]
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
 
K

kankowski

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;
}
 
A

Alf P. Steinbach

* (e-mail address removed):
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top