Sign in to follow this  
Jonus

Help on function pointers plz :)

Recommended Posts

Jonus    170
// 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?

Share this post


Link to post
Share on other sites
orcfan32    100
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'.

Share this post


Link to post
Share on other sites
Jonus    170
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;
}


Share this post


Link to post
Share on other sites
Sandman    2210
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);

Share this post


Link to post
Share on other sites
Jonus    170
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this