Skip to main content
GameDev.net gamedev.net
🔒 Locked

c++, make function pointer in a class call a member function?

Started by johnnyBravo Oct 29, 2004 at 6:34 AM 3 replies 600+ views
Original Post
johnnyBravo
johnnyBravo
Hi, in c++ I've got a function pointer that I want to make equal a member function in a class. For example:

class App {
public:
    void (*fp)();
    void doBeep();
    App();
};

void App::doBeep() {
    Beep(100,50);
}
void App::App() {
    fp = doBeep(); //how do i do this line? 
}



As I can't make the function pointer point to the function of the same class, I am wondering if there is some work around for this? Thanks
Jingo
Jingo
class App {public:    void (App::*fp)();    void doBeep();    App();};void App::doBeep() {    Beep(100,50);}void App::App() {    fp = &App::doBeep; //how do i do this line?     (this->*fp)();}
johnnyBravo
johnnyBravo
thankyou very much!

edit:

Actually I have got one more problem.

For example:

class Base {public:	static void (Base::*fp)();	void doBeep();	Base();	static void myMethod();};void (Base::*Base::fp)() = 0;void Base::doBeep() { Beep(300,70); }void Base::myMethod() {	(this->*fp)(); //I can't get this line to run}Base::Base() {	fp = &Base::doBeep;        myMethod();}


I have tried replacing the this-> with Base:: etc
But it fails to work.

Thankyou.

[Edited by - johnnyBravo on October 29, 2004 8:48:59 AM]
Aprosenf
Aprosenf
The problem is that the function myMethod() is a static member function, so no this pointer exists there. I'm not quite sure why you want myMethod() to be static. Just make myMethod() non-static and it will work.
johnnyBravo
johnnyBravo
Its because in the end the static function is the Windows MsgProc, the static function pointer is the pointer to a msgproc,

and the non static function is my non static msgproc..

I've just used a second class to hold the static function pointer and msgproc now.

Topic Locked

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

Sign in to reply to this topic.