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?
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With