Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does comparing a member function pointer to NULL generate a warning?

The following bit of code compiles without warning for Windows, Mac and iOS:

class MyClass {
    SomeOtherClass * m_object;
    void (SomeOtherClass::*m_callback)();
public:
    MyClass(SomeOtherClass * _object,void (SomeOtherClass::*_callback)()=NULL) :
        m_object(_object),m_callback(_callback) {}

    void DoStuff() {
        //generates warning: NULL used in arithmetic when compiling with the Android NDK
        if (NULL==m_callback) {
            m_object->DoNormalCallback();
        } else {
            (m_object->*m_callback)();
        }
    }
};

Why is that warning generated and what can I do about it?

like image 571
IronMensan Avatar asked Aug 25 '11 14:08

IronMensan


1 Answers

If NULL is defined as ((void*)0), you may get a warning. Object pointers are not type-compatible with function pointers. Use a plain 0 instead of NULL. 0 is a null pointer constant compatible with both function pointer and object pointer types.

EDIT Sorry, I was not paying proper attention. There's a member function pointer here, not just a function pointer. Comparing one with ((void*)0) is also against the rules, and many compilers will issue errors, not just warnings, on this.

EDIT 2 To all who commented: I know that a conforming C++ compiler will not define NULL as ((void*)0). The problem is that there are non-conforming compilers and broken third-party libraries out there (I've seen both).

like image 136
n. 1.8e9-where's-my-share m. Avatar answered Oct 03 '22 06:10

n. 1.8e9-where's-my-share m.