Sign in to follow this  
RiBi

Function pointers and classes [SOLVED]

Recommended Posts

Hello, I just recently run into a problem with function pointers. I though I understand their concepts and used them many times already. However, until now, I have never tried to do the following:
class CLASS;

void (CLASS::*FOO)();

class CLASS {
public:
    void foo() {
        printf("foo");
    }

    void foo2() {
        FOO=&CLASS::foo;
        FOO();        
    }
};

int main() {
    CLASS c;
    c.foo();
    c.foo2();

    return 0;
}



I expected the given code to output two "foo" messages, but on the line with FOO() statement, the compiler informs me with "error C2064: term does not evaluate to a function taking 0 arguments" error message. I went through many articles about function pointers and classes, but none of them have solved my problem so far. What am I doing wrong? Thank you in advance. [Edited by - RiBi on July 8, 2007 2:36:17 AM]

Share this post


Link to post
Share on other sites
foo is a member function (in Object-Oriented jargon, it is called a "message"). When you call it, you must specify an instance on which it is called (the object to which you want to send the "message").

(this->*FOO)();

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this