c++ class with objective-c friend
I have an application thats a mix of c++ and objective-c.
Quite a lot of the c++ classes exist merely as facades to access the underlying objective-c object from the rest of the x++ application.
My problem is one of design: The objective-c class needs to call back into the c++ class via a set of methods I'd prefer to mark as private - no other c++ class (not even derived classes) needs to be messing around with these.
But I can't mark them private, as there doesn't seem to be a way to make objective-c class methods 'friends' of a c++ class.
I considered cheating and using macro's to mark the c++ methods as public when __OBJC__
is defined, but that changes the method's decoration and would result in link errors.
anyone else encounter开发者_如何学编程ed this?
// MyView.mm
@interface MyView : NSView {
@public
CMyView* _cpp;
}
-(void)drawRect:(NSRect)dirtyRect {
CGContextRef cgc = (CGContextRef)[[NSGraphicsContext currentContext]graphicsPort];
_cpp->Draw(cgc);
}
...
// MyView.h
class CMyView {
id _view;
public:
// this method should be private. It exists ONLY for the MyView obj-c class.
void Draw(CGContextRef cdc);
};
If you must do that you can wrap your Obj-C class in a C++ object that is friended to CMyView. You'd need another level of abstraction between the two classes you have already.
精彩评论