Function pointers

Started by
21 comments, last by Graham 20 years, 6 months ago
I can make a function pointer equal to a global function but not to a member function. I can do this: PlayerKeys.W.Func = MoveFoward; I can not do this: PlayerKeys.W.Func = Camera.MoveFoward; Any help?
Go on an Intense Rampage
Advertisement
Man, in order to answer your question you are going to need to give it a little more context. Language? Can you give specifics about the objects you''re using?

Otherwise, check this.

peace and (trance) out

Mage
---------------------------------------------------There are 10 kinds of people in the world:Those that understand binary, and those that dont...Mage
You have to use a pointer to member function, to point to a member function.

You can also use Loki::Functor or Boost::Function to simulate first class functions and closure.

[edited by - Magmai Kai Holmlor on October 7, 2003 10:17:40 PM]
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
I am using c++/ i changed the declaration of my function pointer to: void (CCamera::*Func) (void);
Func is the function pointer.

I initialize it using: PlayerKeys.W.Func = CCamera::MoveFoward;

I have no errors intitializing it now, only problems calling the function. I tried calling like this:
Camera.*(PlayerKeys.W.Func) ();

it did not work though, i tried a few different syntaxes, but always fail.
Go on an Intense Rampage
I had the exact same problem as you. Some guy helped me out, I''d give him credit but I don''t remeber his name and I can''t find my original post anywhere...

But here''s how to call the function:

(this->*funcName)();

In your case I think it would be:
(this->*PlayerKeys.W.Func)();

It seems like you''re trying to call the function through an instance of your class (externally)? Because I''m not sure what the rules are for calling public members that point to private members...

Anyway, I hope that works for you.
I cant use ''this->'' because im not calling it from within the class. Someone with a good knowledge of c++ must know it.
Go on an Intense Rampage
IIRC, the parentheses do matter.

(object.*mem_fun_ptr)()
(pointer->*mem_fun_ptr)()

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"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
I don''t think it can be done. When I tried it in gcc, it said:

error: ISO C++ forbids taking the address of a bound member
function to form a pointer to member function.

Don''t quote me on this, but I think it can only be done from within a class or if its static, though that seems kind of silly. If the function''s static then the following code works:
#include <iostream>using namespace std;void func() {	cout << "Global function\n";}class cls {	public:	static void clsfunc() {		cout << "Member function\n";	}};void execute (void function()) {	(*function)();}int main() {	execute(func);	cls clas;	execute(cls::clsfunc);	return 0;}

I am interested if anyone can get it to work without making it static or calling it from within itself.
Zorx (a Puzzle Bobble clone)Discontinuity (an animation system for POV-Ray)
#include <iostream>class TestClass {public:  int x;  TestClass() : x(0) {};  void DoIt() { ++x; };};using namespace std;int main() {  TestClass tc;  void (TestClass::*func)();  func = &TestClass::DoIt;  cout << "x was " << tc.x << endl;  (tc.*func)();  cout << "x is " << tc.x << endl;  return 0;} 
my situation is more like:

class TestA
{
public:
int x;
void DoIt(void)
{ x++; }
};

class TestB
{
public:
void (TestA::*func)(void);
};

int main()
{
TestA varA;
TestB varB;

varA.x = 1;
varB.func = TestA::DoIt;

varB.func ()
return 0;
}

i get a compile error on line "varB.func ()"

Go on an Intense Rampage

This topic is closed to new replies.

Advertisement