Passing function as argument

S

steven_orocos

Hi,

I'm tryin to pass a funtion as an argument in another funtion.

This code works
----------------------------------
typedef void (*func)(int);

void test1(int a){cout<<"1";}
void test2(int a){cout<<"2";}
void test3 (void (*fp) (int)) {
(fp)(5);
}
void test() {
func pf;
pf = test1;
test3(test1);
}
--------------------------------
But when I put the cide in a class, it won't work anymore.
-------------------------------
typedef void (*func)(int);

void MyClass::test1(int a){cout<<"1";}
void MyClass::test2(int a){cout<<"2";}
void MyClass::test3 (void (*fp) (int)) {
(fp)(5);
}
void MyClass::test() {
func pf;
pf = test1;
test3((func)test1);
}
-----------------------
Gives me the error
--------------
error: argument of type 'void (MyClass::)(int)' does not match
'void (*)(int)'
error: invalid use of member (did you forget the '&' ?)
--------------------

I've tried several things, like
test3(&test1);
or
typedef void (MyClass::*func)(int);


But i can't find the solution.
Also the net gives me several different solution, but none work..
 
O

Ondra Holub

(e-mail address removed) napsal:
Hi,

I'm tryin to pass a funtion as an argument in another funtion.

This code works
----------------------------------
typedef void (*func)(int);

void test1(int a){cout<<"1";}
void test2(int a){cout<<"2";}
void test3 (void (*fp) (int)) {
(fp)(5);
}
void test() {
func pf;
pf = test1;
test3(test1);
}
--------------------------------
But when I put the cide in a class, it won't work anymore.
-------------------------------
typedef void (*func)(int);

void MyClass::test1(int a){cout<<"1";}
void MyClass::test2(int a){cout<<"2";}
void MyClass::test3 (void (*fp) (int)) {
(fp)(5);
}
void MyClass::test() {
func pf;
pf = test1;
test3((func)test1);
}
-----------------------
Gives me the error
--------------
error: argument of type 'void (MyClass::)(int)' does not match
'void (*)(int)'
error: invalid use of member (did you forget the '&' ?)
--------------------

I've tried several things, like
test3(&test1);
or
typedef void (MyClass::*func)(int);


But i can't find the solution.
Also the net gives me several different solution, but none work..

You are trying to pass pointer to member of class as parameter. It is
something different, than pointer to ordinary function. But you can
still pass pointer to static member function - it works same way as
ordinary function, because it does not need 'this' value for invocation.
 
J

Jacek Dziedzic

Hi,

I'm tryin to pass a funtion as an argument in another funtion.

This code works
----------------------------------
typedef void (*func)(int);

void test1(int a){cout<<"1";}
void test2(int a){cout<<"2";}
void test3 (void (*fp) (int)) {
(fp)(5);
}
void test() {
func pf;
pf = test1;
test3(test1);
}
--------------------------------
But when I put the cide in a class, it won't work anymore.
-------------------------------
typedef void (*func)(int);

void MyClass::test1(int a){cout<<"1";}
void MyClass::test2(int a){cout<<"2";}
void MyClass::test3 (void (*fp) (int)) {
(fp)(5);
}
void MyClass::test() {
func pf;
pf = test1;
test3((func)test1);
}
-----------------------
Gives me the error
--------------
error: argument of type 'void (MyClass::)(int)' does not match
'void (*)(int)'
error: invalid use of member (did you forget the '&' ?)
--------------------

I've tried several things, like
test3(&test1);
or
typedef void (MyClass::*func)(int);


But i can't find the solution.
Also the net gives me several different solution, but none work..

Pointers to members differ from pointers to ordinary functions
(and one cannot be cast to another), because member functions take
an extra "hidden" 'this' argument.

HTH,
- J.
 
S

steven_orocos

Roland Pibinger schreef:

I had allready found that link
using this code
test3(*this.*test1);
I get error: '((MyClass*)this)->MyClass::test1' cannot be used as a
member pointer, since it is of type '<unknown type>'
And this
test3((void(*)(int))*this.*test1);
gives me
error: invalid cast from type 'MyClass' to type 'void (*)(int)'
 
D

David Harmon

On 27 Nov 2006 04:47:26 -0800 in comp.lang.c++,
(e-mail address removed) wrote,
typedef void (MyClass::*func)(int);
void MyClass::test1(int a){cout<<"1";}
void MyClass::test2(int a){cout<<"2";}
void MyClass::test3 (void (*fp) (int)) {

void MyClass::test3 (func fp) {

(this->*fp)(5);
}
void MyClass::test() {
func pf;
pf = test1;

pf = &test1;
test3((func)test1);

test3(&test1);

This issue is covered in Marshall Cline's C++ FAQ. See the section
"[33.5] How can I avoid syntax errors when calling a member function
using a pointer-to-member-function? " but ignore his #define. It is
always good to check the FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top