c ++类与objective-c的朋友 [英] c++ class with objective-c friend
问题描述
我有一个混合使用c ++和objective-c的应用程序。
I have an application thats a mix of c++ and objective-c.
很多c ++类只是作为门面访问底层目标对象从x ++应用程序的其余部分。
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.
我的问题是设计之一:目标c类需要通过一组方法调用回c ++类'd喜欢标记为私有 - 没有其他c ++类(甚至派生类)需要解决这些。
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.
但我不能标记他们的私人,因为似乎没有办法使一个c ++类的objective-c类方法的朋友。
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.
我考虑作弊和使用宏标记的c ++方法作为public,当定义 __ OBJC __
时,会更改方法的修饰,并导致链接错误。
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.
任何人否则遇到这个?
// 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 OnPaint(CGContextRef cdc);
};
推荐答案
如果你必须这样做, C类在一个C ++对象是亲爱的CMyView。
你必须在两个类之间有另一个抽象层次。
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.
这篇关于c ++类与objective-c的朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!