Help on function pointers plz :)

Started by
5 comments, last by Janju 18 years, 9 months ago
// test04funktionzeiger.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int test(int a, int (*func)(int))
{
	std::cout << (*func)(a);

	return 0;
}

int sum(int i)
{
	return i+i;
}

class Foo
{
public:
	double One( long inVal ){return inVal;}
	double Two( long inVal ){return inVal;}
	double Three( double (Foo::*f)(long) )
	{		 
		return (*f)(1); // <--- why does this not work?

		// coumpiler output:
		// error C2171: '*' : illegal on operands of type 'double (__thiscall Foo::* )(long)'
		// error C2064: term does not evaluate to a function taking 1 arguments
	}
};

int main( int argc, char **argv )
{
	double (Foo::*funcPtr)( long ) = &Foo::One;
	Foo aFoo;
	double result =(aFoo.*funcPtr)( 2 ); 

	aFoo.Three(&Foo::Two);

	test(2,&sum);

	return 0;
}


Can someone tell me why my function Three won't work or what's the matter?
Advertisement
Y'know, I didn't really pay attention to the pointer's lessons that I find on the internet.. :) Have you tried removing the '*'? If that doesn't work, I can't help you. It looks like your casting the 'f'.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
If I remember well (been a long time since C), I think you should do this

return f(1); and not return (*f)(1)
Quote:Original post by Ali_B
If I remember well (been a long time since C), I think you should do this

return f(1); and not return (*f)(1)
If i change it that way this is the not working result:
#include "stdafx.h"#include <iostream>using namespace std;int test(int a, int (*func)(int)){	std::cout << (*func)(a);	return 0;}int sum(int i){	return i+i;}class Foo{public:	double One( long inVal ){return inVal;}	double Two( long inVal ){return inVal;}	double Three( double (Foo::*f)(long) )	{		 		return f(1); // <--- why does this not work?		// coumpiler output:		// C2064: term does not evaluate to a function taking 1 arguments	}};int main( int argc, char **argv ){	double (Foo::*funcPtr)( long ) = &Foo::One;	Foo aFoo;	double result =(aFoo.*funcPtr)( 2 ); 	aFoo.Three(&Foo::Two);	test(2,&sum);	return 0;}


Oh. That's C and not C++...
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
In order to call function pointers to member functions, you have to invoke some strange arcane sytax, which in your case will look something like this:

return (this->*f)(1);
AFAIK the topic should be callback functions in c++ or something like that.

#include "stdafx.h"#include <iostream>using namespace std;int test(int a, int (*func)(int)){	std::cout << (*func)(a);	return 0;}int sum(int i){	return i+i;}class Foo{public:	double One( long inVal ){return inVal;}	double Two( long inVal ){return inVal;}	double Three( double (Foo::*f)(long) )	{		 		return (this->*f)(1); // <--- Ha, Sandman you saved the day!!! I own you one.	}};class Foo2{public:	static double One( long inVal ){return inVal;}	static double Two( long inVal ){return inVal;}	static double Three( double (*f)(long) )	{		 		return (*f)(3); // <--- works!	}};int main( int argc, char **argv ){	// test for foo (instanced class)	double (Foo::*funcPtr)( long ) = &Foo::One;	Foo aFoo;	double result =(aFoo.*funcPtr)( 2 ); 	aFoo.Three(&Foo::Two);	// test for foo2 (static class)	double (*funcPtr2)( long ) = &Foo2::One;	double result2 =(*funcPtr2)( 2 );	Foo2::Three(&Foo2::Two);	// test for global function	test(2,& sum);	return 0;}
With a static class everything works as expected.

Thanks Sandman now i can sleep better again. As far as i remember x->y translates into (*x).y:
#include <iostream>class test{public:	void a(){std::cout << "test!" << std::endl;};};int _tmain(int argc, _TCHAR* argv[]){	test * a = new test;	a->a();	(*a).a();	delete a;	return 0;}


But if you try to write it that way in that callback fuction pointer case the compiler refuses to take it.

Indeed arcane.

[Edited by - Jonus on July 18, 2005 10:45:20 AM]

This topic is closed to new replies.

Advertisement