I have a c++ class implemented in a objective-c++ (.mm) file.
This class contains some Cocoa object, say a NSToolbar
, as a (private) member variable.
The class should be exposed as a pure c++ interface and usable by pure c++ clients.
In other words, I am trying to wrap obj-c objects in a c++ class.
The first thing that crosses my mind is to use a void pointer in the class interface
and then work around with casting within the class implementation, whenever _toolbar
needs to be treated as a NSToolbar
.
For example I would have the interface:
// NSToolbarWrapper.h
class NSToolbarWrapper {
private:
void * _toolbar;
//... rest of the class ...
}
and the implementation:
// NSToolbarWrapper.mm
...
ToolbarWrapper::ToolbarWrapper (...){
_toolbar = (__bridge void *)[[NSToolbar alloc] initWithIdentifier:@"My toolbar!"];
...
}
I am not sure this is the smartest approach. Is there a best practice in this case?