Archived

This topic is now archived and is closed to further replies.

Graham

Function pointers

Recommended Posts

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?

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

Share this post


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

Share this post


Link to post
Share on other sites

#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;
}

Share this post


Link to post
Share on other sites
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 ()"

Share this post


Link to post
Share on other sites
Of course you do. func still refers to a member function of class TestB; it can''t be called on an object of type TestA. You''ll need to use the arrow-star and dot-star operators for calling a member function by its pointer.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
No, you wouldn''t have to create a template functor class. That''s for convenience. But the syntax they use there is the syntax you want. Storing a member function pointer and calling it on an object. I could write you a code sample, or show you an example, but I would just be duplicating the code there, minus some extra enhancements they throw on. Try to work through it a little bit. Omit the template stuff.

Share this post


Link to post
Share on other sites
quote:
Original post by Graham
Can anyone provide an answer that works for my example.


class CCamera
{
public:
static void MoveForward(CCamera &Camera);
void MoveForward();
};

static void CCamera::MoveForward(CCamera &Camera)
{
Camera.MoveForward();
};

PlayerKeys.W.Func = CCamera::MoveFoward;
...
PlayerKeys.W.Func(Camera);


Cheers!

Share this post


Link to post
Share on other sites
I am somewhat new to c++ and just found a solution for this that in my own project might help you. I don''t know if it is the right way but it works for me.

After going through the all the stuff to declare and set a member function pointer, create a member public inline function that wraps the (this->*MemberFunctionPointerName)() call. That way you have simplified the syntax of function pointers, allowed external access without adding additional overhead.

Lee Moore

Share this post


Link to post
Share on other sites
Graham, I think it should work like you did it in your "Test" application here.

Though I think I did it once but when I passed the adress to the function I did it like this:

&CSomeClass::SomeFunction;

Share this post


Link to post
Share on other sites