What does this mean: warning: converting from ‘void (ClassName::*)()’ to ‘void (*)()’

Posted by Brendan Long on Stack Overflow See other posts from Stack Overflow or by Brendan Long
Published on 2010-03-27T02:50:02Z Indexed on 2010/03/27 2:53 UTC
Read the original article Hit count: 759

Filed under:
|
|

I have a member function in a class that has a callback, but the callback isn't strictly neccessary, so it has a default callback, which is empty. It seems to work fine, but I get an annoying warning:

warning: converting from ‘void (ClassName::*)()’ to ‘void (*)()’

I'm trying to figure out what it means and how to turn it off (or fix it if I really am doing something wrong). Here's some simple code:

class ClassName{
public:
    void doSomething(void (*callbackFunction)() = (void(*)()) &ClassName::doNothing){
        callbackFunction();
    }
    void doNothing(){}
};

int main(){
    ClassName x;
    x.doSomething();
    return 0;
}

Note: If I do this (without explicitly casting it as a void(*)()):

void doSomething(void (*callbackFunction)() = &ClassName::doNothing)

I get this:

main.cpp:3: error: default argument for parameter of type ‘void (*)()’ has type ‘void (ClassName::*)()’

© Stack Overflow or respective owner

Related posts about g++

Related posts about c++