Skip to main content
gamedev.net
🔒 Locked

Functors: Simple question

Started by jollyjeffers 2 replies 500+ views
Original Post
jollyjeffers
jollyjeffers
hi all, I've searched these forums and found this tutorial: functor.html while trying to get function pointers working... but for some reason it really doesn't seem to want to work..
template< class _C >
class MY_MouseEventHandler {

private:
	bool ( _C::*fPtr )( int b, int x, int y );
	_C* thisPtr;

public:
	void Call( int b, int x, int y ) {
		(*thisPtr.*fPtr)(b,x,y);
	}

	MY_MouseEventHandler( _C* classPtr, bool ( _C::*functionPtr )( int b, int x, int y ) ) {
		fPtr = functionPtr;
		thisPtr = classPtr;
	}

};
I use the above code as my functor - I know most people use a base class, but for this particular job that seemed to be overkill.. This code compiles with no complaints, and is a near-enough copy of the code from the above link.
class TEST_A {

public:
	void init( ) { };

	bool MouseDown( int b, int x, int y ) { };

	bool MouseUp( int b, int x, int y ) { };

	bool MouseMove( int b, int x, int y ) {	};
	
};
I use this class as a simple test - to represent a set of functions that might receive the callback events.. I then, in my main() have this code - the stuff that actually uses the two previous classes:
TEST_A t_a;
t_a.MouseDown( 1, 2, 3 );
t_a.MouseMove( 4, 5, 6 );
t_a.MouseUp( 7, 8, 9 );

MY_MouseEventHandler< TEST_A > meh( &t_a, &t_a::MouseDown ); 
Thing is, it wont compile - I get an error on the last line: error C2510: 't_a' : left of '::' must be a class/struct/union okay, I know what the error means... but I don't get why it won't allow the above code?! it's really confusing me. The code in the aforementioned tutorial does a nearly identical thing. Any ideas? cheers, Jack [edited by - Jollyjeffers on January 29, 2004 7:29:59 PM] [edited by - Jollyjeffers on January 29, 2004 7:30:14 PM] [edited by - Jollyjeffers on January 29, 2004 7:31:11 PM]
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
SiCrane
SiCrane
t_a isn''t a struct, class or union. It''s an object. Try replacing &t_a::MouseDown with &TEST_A::MouseDown.
Fruny
Fruny
MY_MouseEventHandler<TEST_A> meh( &t_a, &TEST_A::MouseDown );

Since you''ve found a functor tutorial, now look for a member-pointer tutorial, both bound and unbound.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
jollyjeffers
jollyjeffers
cheers for the answers guys...

I''ll blame my stupidity on it being quite late / having done a C14 the night before... but well, maybe I am just a fool

Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.