Question

Why does the compiler treat &Foo::foo as void (*)(). I am expecting it to be treated as void(Foo::*)() instead since it's a member of Foo.

class Foo
{
    public:
        static void foo ( void ){}
};

void foo ( void(Foo::*)(void) ){}

int main()
{   
    foo(&Foo::foo);  // error: cannot convert ‘void (*)()’ to ‘void (Foo::*)()’
    return 0;
}
Was it helpful?

Solution

You declared the function foo as static.

It is therefore not a member function of a Foo instance.

This code works:

class Foo
{
public:
    static void foo ( void ){}
    void foo2() {}
};

void foo ( void(*)(void) ){}
void fooMember ( void(Foo::*)(void) ){}

int main()
{   
    foo(&Foo::foo);
    fooMember(&Foo::foo2); 
    return 0;
}

EDIT: I updated the description and added a piece of code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top