I've got an interesting problem that I am sure someone would have come across. I am writing the front end UI in Objective C Cocoa, and the backend in C++. In C++ I have
#define NULL 0
Unfortunately, this has dire consequences for nil. Especially with nil terminated function calls as I now get this warning - "Missing sentinel in method dispatch", which I assume means it couldn't find the nil terminator. This is the only definitio开发者_运维知识库n I could find for nil:
#ifndef NULL
#define NULL __DARWIN_NULL
#endif /* ! NULL */
#ifndef nil
#define nil NULL
#endif /* ! nil */
which seems to me that nil is NULL, and that my earlier define for NULL is messing everything up although I don't know how. The NULL is defined in C++ so that it can be platform independent. I have tried redefining NULL and nil, but nothing seems to take. Any suggestions on the correct way to go about this would be appreciated.
In either C or C++, attempting to define NULL
yourself leads to undefined behavior. Sorry, but you're just allowed to do that. Instead of trying to define it yourself, you need to include one of the headers that already defines it for you.
Reminded me of this question I saw recently: How to wrap a C++ lib in objective-C?
So, what about:
#ifdef __cplusplus
#define NULL 0
#endif
You could #define it while it is being used within your .h/.cpp. Then, at the end of those files, #undef NULL
so that the ObjC's definitions take over. That way the definition is cleaned-up.
You can also simply use 0
in C++ instead of NULL
.
精彩评论