Skip to main content
gamedev.net
🔒 Locked

Calling member functions through pointers

Started by SpaceRogue May 22, 2003 at 12:09 PM 3 replies 700+ views
Original Post
SpaceRogue
SpaceRogue
I'm having a little trouble with pointers to member functions. I have this class with two member functions and a data member which is a pointer to a function like this:
class Thingie
{
public:
	bool (Thingie::*funcpointer)();
	bool funcA();
	bool funcB();
	Thingie();
	virtual ~Thingie();
};
  
I know I've set funcpointer=funcA successfully as debugging reveals the correct address is stored in funcpointer, but I can't seem to call the function from outside the class using something like:
Thingie A;
(A.*funcpointer)(); 
That will not compile, giving me an error that funcpointer is an undeclared identifier. If I change it to:
(A.funcpointer)(); 
or
*(A.funcpointer)(); 
I get an error that the expression does not evaluate to a function. What am I doing wrong here? [edited by - SpaceRogue on May 22, 2003 1:11:19 PM]
Spacecat
Spacecat
I recommend you look up this site:
http://www.newty.de/CCPP/callback/callback.html

They explain why you -can''t- cleanly use a member function of a class with a callback pointer, and a workaround.
=^.^= Leaders and teachers should remember: It is best to offer others what they Need, not what they Want.
SpaceRogue
SpaceRogue
Actually I''ve already been using that site as a guide, but still couldn''t get the syntax right until just a few minutes ago.

I finally figured out it has to be:
(A.*(A.funcpointer))() 


This works, but certainly is ugly.

A wrapper might look better.

daerid
daerid
I like member function wrappers

  
template< typename _Type, typename _Result, typename _Arg >
class MemberFunction
{
public:
MemberFunction( _Result (_Type::*pfn)( const _Arg& ) ) :
mPfn( pfn ) {}

_Result operator()( _Type& obj, const _Arg& a ) {
}
private:
_Result (_Type::*mPfn)( const _Arg& );
};



class test
{
public:
void DoSomething( const std::string& out ) {
std::cout << "test::DoSomething( " << out << " );" << std::endl;
std::cout << "test::i = " << this->i << std::endl;
}
int i;
};

void main() {
test t;
t.i = 4;

MemberFunction< test, void, std::string > pfn( test::DoSomething );
pfn( t, "test" );

}



Kind of contrived, and also, there's stdlib headers that allow for the same thing. Take a look at . Also, boost::function is pretty neat too.

[edited by - daerid on May 22, 2003 2:20:46 PM]
daerid@gmail.com
SpaceRogue
SpaceRogue
Thanks, although I think your example is a bit beyond what I'm capable of understanding at the moment.

I've only been programming for three months, so I'm just happy to have made it as far as I have. Templates still give me headaches from just looking at them. Although I understand the concept just fine, I still find all but the simplest ones illegible.

[edited by - SpaceRogue on May 22, 2003 4:59:15 PM]

Topic Locked

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

Sign in to reply to this topic.