C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Callback member functions

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Fri Jun 23, 2006 7:50 am    Post subject: Callback member functions Reply with quote



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? Smile
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;
}
Back to top
Victor Bazarov
Guest





PostPosted: Fri Jun 23, 2006 8:23 am    Post subject: Re: Callback member functions Reply with quote



kankowski (AT) gmail (DOT) com wrote:
Quote:
[..]
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?

Quote:
}

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
Back to top
Guest






PostPosted: Fri Jun 23, 2006 8:25 am    Post subject: Re: Callback member functions Reply with quote



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;
}
Back to top
Alf P. Steinbach
Guest





PostPosted: Fri Jun 23, 2006 8:25 am    Post subject: Re: Callback member functions Reply with quote

* kankowski (AT) gmail (DOT) com:
Quote:
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? Smile
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/.


Quote:
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?
Back to top
Phlip
Guest





PostPosted: Fri Jun 23, 2006 8:28 am    Post subject: Re: Callback member functions Reply with quote

kankowski wrote:

Quote:
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.

http://www.parashift.com/c++-faq-lite/pointers-to-members.html

Always check the FAQ before posting! Read 33.2.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Back to top
Guest






PostPosted: Fri Jun 23, 2006 8:31 am    Post subject: Re: Callback member functions Reply with quote

Thank you, it works now. Many thanks!
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.