Compiler differences between Obj-C and Obj-C++
I recently had to change an existing class from Obj-C to Obj-C++. After changing the file type, I suddenly got an error from this line:
CFDataGetBytes(MACAddressAsCFData, CFRangeMake(0, kIOEthernetAddressSize), MACAddress);
No matching function for call to CFDataGetBytes
Long story short, it turns out the problem is with the type of the first argument:
CFTypeRef MACAddressAsCFData;
The Obj-C++ is more rigorous in checking the type. In CFData.h CFDataGetBytes
is defined like this:
void CFDataGetBytes(CFDataRef theData, CFRange range, UInt8 *buffer);
Notice it states CFDataRef
rather than CFTypeRef
- even though CFTypeRef
is a void *
, the Obj-C++ compiler doesn't accept it. So, the solution is simply
CFDataGetBytes((CFDataRef)MACAddressAsCFData, CFRangeMake(0, kIOEthernetAddressSize), MACAddress);
Written by Benjamin Schuster-Boeckler
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Mac
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#