Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Callback member functions

Reply
Thread Tools

Callback member functions

 
 
kankowski@gmail.com
Guest
Posts: n/a
 
      06-23-2006
Hi, I have two methods A::Conv1 and A::Conv2 that transform coordinates
in two different ways. A:raw() 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:raw(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;
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-23-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> [..]
> void A:raw(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


 
Reply With Quote
 
 
 
 
kankowski@gmail.com
Guest
Posts: n/a
 
      06-23-2006
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:raw(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;
}

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      06-23-2006
* (E-Mail Removed):
> Hi, I have two methods A::Conv1 and A::Conv2 that transform coordinates
> in two different ways. A:raw() 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?
 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      06-23-2006
kankowski wrote:

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


http://www.parashift.com/c++-faq-lit...o-members.html

Always check the FAQ before posting! Read 33.2.

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


 
Reply With Quote
 
kankowski@gmail.com
Guest
Posts: n/a
 
      06-23-2006
Thank you, it works now. Many thanks!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
Member function pointers to member functions with default arguments Hamish C++ 3 01-25-2008 06:46 AM
Member Callback Functions cadull C++ 1 05-06-2005 01:51 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments