Function pointers

J

Jakob Vesterstrom

Hi all

I have question regarding function passing.

I have class A

class A {
public:
...
void attach(double f(double in)) {
this->f = f;
}
double (*f) (double in);
};

with a method 'attach', which takes a function and attaches it to the
class member f. It is quite simple to attach the function 'afunc'

double afunc(double x) {
return x*x;
}

to A::f by the statements

A a;
a.attach(afunc);

However, if I declare a new class with a function member also called 'afunc'

class B {
public:
...
double afunc(double x) {
return x+x1;
}
};

and tries to do the attachment in the same manner

A a;
a.attach(b.afunc);

then I get this error from the compiler:

test.c: In function `int main()':
test.c:33: error: no matching function for call to `A::attach(<unknown type>)'
test.c:8: error: candidates are: void A::attach(double (*)(double))

My question(s) is(are):

Why is the type of b.afunc unknown to the compiler?
What is the best generic fix to this problem?

Regards,
Jakob Vesterstrom
 
S

Sumit Rajan

Jakob Vesterstrom said:
Hi all

I have question regarding function passing.

I have class A

class A {
public:
...
void attach(double f(double in)) {
this->f = f;
}
double (*f) (double in);
};

with a method 'attach', which takes a function and attaches it to the
class member f. It is quite simple to attach the function 'afunc'

double afunc(double x) {
return x*x;
}

to A::f by the statements

A a;
a.attach(afunc);

However, if I declare a new class with a function member also called 'afunc'

class B {
public:
...
double afunc(double x) {
return x+x1;
}
};

and tries to do the attachment in the same manner

A a;
a.attach(b.afunc);

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

Regards,
Sumit.
 
J

John Harrison

Jakob Vesterstrom said:
Hi all

I have question regarding function passing.

I have class A

class A {
public:
...
void attach(double f(double in)) {
this->f = f;
}
double (*f) (double in);
};

with a method 'attach', which takes a function and attaches it to the
class member f. It is quite simple to attach the function 'afunc'

double afunc(double x) {
return x*x;
}

to A::f by the statements

A a;
a.attach(afunc);

However, if I declare a new class with a function member also called 'afunc'

class B {
public:
...
double afunc(double x) {
return x+x1;
}
};

and tries to do the attachment in the same manner

A a;
a.attach(b.afunc);

then I get this error from the compiler:

test.c: In function `int main()':
test.c:33: error: no matching function for call to `A::attach(<unknown type>)'
test.c:8: error: candidates are: void A::attach(double (*)(double))

My question(s) is(are):

Why is the type of b.afunc unknown to the compiler?

You shouldn't take compiler error messages too literally.

The error is that you are passing a member function pointer where a function
pointer is expected. These two things are incompatible and there is no way
to convert from one to the other.
What is the best generic fix to this problem?

The best generic fix is to use templates to write a functor library, but I
suspect that would be solving a different problem from the one you think you
are asking about.

A very simple fix in your case would be to make B::afunc static.

You should look at the FAQ on this topic

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

john
 
M

Martin Gieseking

I have class A

class A {
public:
...
void attach(double f(double in)) {
this->f = f;
}
double (*f) (double in);
};
However, if I declare a new class with a function member also called
'afunc'

class B {
public:
...
double afunc(double x) {
return x+x1;
}
};
and tries to do the attachment in the same manner

A a;
a.attach(b.afunc);


You can't use pointers to non-static methods like "normal" functions. The
pointer type must always contain the appropriate class specifier, e.g.

void (B::*f)(double);

This is because you can only invoke a method in conjunction with an
object using the .* or ->* operator:

B b;
f = &B::afunc;
(b.*f)(1.0);

So there is no way to assign a non-static member function to an ordinary
function pointer.

Martin
 

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
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top