2010-09-27 159 views
0

我正在研究一个只需要重写的项目,但这不是一个选项。从C++函数访问Objective-C变量

我有一个C++函数被调用,并做各种东西。我需要它从App Delegate类读取一个变量。

比如我有:

@interface MyAppDelegate : NSObject <UIApplicationDelegate> 
{ 
    UIWindow *window; 
    MyViewController *viewController; 

int mToleranceLevel; 
} 

然后我有一个需要访问mToleranceLevel功能:

bool FindExtrinsics(...) 
{ 
float maxError = mainDelegate.mMaxError; 
     ... 
} 

的问题是,这个被宣布像这样:

@interface MyClass : UIViewController 
{ 
    ... 
} 

@properties ... 

bool FindExtrinsics(...); 

@end 

那么我怎么会从AppDelegate类中获得一个值。我知道如何获得当前代表:

mainDelegate = (RedStripeARAppDelegate *)[[UIApplication sharedApplication] delegate]; 

但是,如何使用此信息来获取我的C++函数中的值。有没有办法让一个静态变量,所以我可以调用MyAppDelegate.mToleranceValue; ??

+0

的AppDelegate * AD =((AppDelegate中*)CCApplication :: sharedApplication()); – 2014-07-09 07:46:51

回答

2

Xcode支持Objective-C++,它使您能够使用C++代码中的Objective-C调用。将C++代码文件的扩展名从.cpp(或.cc)更改为.mm,您将可以从Objective-C代码中获取C++代码中的值。

0

根据编译器和运行时的不同,Objective C对象通常只是一个指向结构的指针,实例变量可能只是该C结构的成员。和C的语法目标C的适当子集所以从C或C++可以是简单的作为对象的变量访问:

if (myObject != NULL) { 
    x = myObject->myInstanceVariable; 
} 
+0

考虑到你需要在你的C++代码中包含Objective-C接口,如果你想要这样做,你需要Objective-C++,所以你可能也想使用常规访问器。 – zneak 2010-09-27 22:44:18