Skip to main content
gamedev.net
🔒 Locked

Using pointers to functions

Started by knyot Feb 16, 2003 at 1:51 AM 8 replies 300+ views
Original Post
knyot
knyot
I''m writing a keyboard listening class that uses pointers to functions. The pointers are to functions that are listening for keyboard input. Each time around, the pointers "call" the functions with the keys that have been pressed. It works fine with a function not from a class... however when I try to register a function that''s a member of a class, I get an error because I''m trying to cast from "void (ClassA::*)(const char*)" to "void (*)(const char*)" Is there something I can do to fix this? I can''t declare ClassA''s function as static either. Thanks for any help.
RhoneRanger
RhoneRanger
(void*)&const char; I think would work.
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
knyot
knyot
I'm not sure what you mean...

do you mean to register the pointers as:
void (*&)(const char[])

[edited by - knyot on February 16, 2003 3:09:40 AM]
RhoneRanger
RhoneRanger
I think what you are trying to do is just plain ugly code.

Why not just have a function called CheckInput();(or something) place it in your main loop and test for key handlers there?

TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
Dresden
Dresden
To the best of my knowledge it isnt possible in C++ to make a function pointer to a non-static class method because of the methods hidden ''this'' parameter
knyot
knyot
I realize it''s ugly... it started out as one thing and manifested into something else. I was going to use this to make sure only the parts of the program with focus are handling the keyboard input rather than everyone reacting to it. I guess I''ll do it another way though.

thanks for the help.
Gleno
Gleno
kynot, as you are using c++ it is best to use the features of c++ and not hamper yourself with the methods such as function pointers best suited to c. While in some instances function pointers can be useful in C++, messageing is not one of them.

Use a pure virtual base class which has the one handle event or keyboard event function. Have your listening class inherit this base class and implement the handle event function. Your listening class can then pass itself to the keyboard object. Effectively saying, notify ME of events.

Like this.

class KeyboardListener{
public:
virtual void keyboardEvent(KeyboardEventData *data) = 0;
};



class MyImportantKeyboardListeningObject public: KeyboardListener{

public:
MyImportantKeyboardListeningObject()
{
aPointerToKeyboardObject->registerListener(this);
}

void keyboardEvent(KeyboardEventData *data)
{
//ah the keyboard has done some cool stuff
}

};

This is pretty rough, and you would probably want to make the base class more robust to handle different kinds of events, so you can pass yourself around to a whole host of objects to get events from, but hopefuly it illustrates the process.

Just make sure that your keyboard or input object knows what a KeyboardListener is.

Oh not to miss this important point. Your keybord object will add the keyboardListener to a KeyBoardListener pointer or list of pointers, and make it call all of the listeners when events happen.

Hope this helps, and is clear.
knyot
knyot
Thanks so much Gleno!! That''s exactly what I was trying to do but yours is so much more elegant (and functional haha). I guess I''m still picking up the finer points of OOP with C++.

Thanks again!!!
Arild Fines
Arild Fines
quote:
Original post by Dresden
To the best of my knowledge it isnt possible in C++ to make a function pointer to a non-static class method because of the methods hidden ''this'' parameter

It is quite possible. However, you need to provide the this parameter when calling it:

class Foo
{
public:

void Method1();
void Method2();
void Method3();
};


//...

typedef void(Foo::*FooMethod)();

// the method pointer, pointing to Method1
FooMethod f = &Foo::Method1;

// the object we want to invoke the method on
Foo foo = new Foo();

//invoke the method ptr on the foo object
(foo->*f)();


Yes, it''s ugly... I''d go with Gleno''s suggestion.



"I know very well who Satan is: He is freedom. He is the uncontrolled, the incalculable, the antithesis of order and discipline, the antithesis of the legalism of outer space.... We know where a planet will be in twelve years, four months and nine days. But we don''t know where a butterfly will have flown one minute hence. Therefore the butterfly is of Satan."
-- Jens Bjørneboe
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

Topic Locked

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

Sign in to reply to this topic.