Jump to content
  • Advertisement

Archived

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

Graham

Function pointers

This topic is 5368 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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

Share this post


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

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
I cant use ''this->'' because im not calling it from within the class. Someone with a good knowledge of c++ must know it.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!