我只是在學習Objective C(和Objective-C++),我有一個Objective-C++類,它具有以下構造函數。從目標c調用目標C++方法
void InputManager::_init (int inputAreaX, int inputAreaY, int inputAreaWidth, int inputAreaHeight)
如何從目標C中調用它?
我只是在學習Objective C(和Objective-C++),我有一個Objective-C++類,它具有以下構造函數。從目標c調用目標C++方法
void InputManager::_init (int inputAreaX, int inputAreaY, int inputAreaWidth, int inputAreaHeight)
如何從目標C中調用它?
這似乎是一個純粹的C++方法,所以它的工作方式與普通C++(即使在Objective-C++文件中)完全相同。例如,你可能已經在棧上定義的變量:
InputManager mgr; // or, include constructor arguments if the class can't be default-constructed
mgr._init(x, y, w, h); // this assumes 4 variables exist with these names; use whatever parameter values you want
名稱_init
是雖然有點怪異;你的意思是這是一個類的構造函數嗎?如果是這樣,則應該定義InputManager::InputManager(int x, int y, int w, int h)
。
如果你真的希望這個類是Objective-C只有,它的語法和行爲是不同的。
你有兩個選擇:
選項1.
翻譯成Objective-C的唯一代碼。我不是C++那麼好,但是這可能是什麼樣子的.H:
-(id)initWithAreaX: (int) inputAreaX AreaY: (int) inputAreaY AreaWidth: (int) inputAreaWidth AreaHeight: (int) inputAreaHeight;
因爲它看起來像這是一個構造函數,它可能會像這樣在執行:
-(id)initWithAreaX: (int) inputAreaX AreaY: (int) inputAreaY AreaWidth: (int) inputAreaWidth AreaHeight: (int) inputAreaHeight {
self = [super init];
if(self) {
//Custom Initialization Code Here
_inputAreaX = inputAreaX;
_inputAreaY = inputAreaY;
_inputAreaWidth = inputAreaWidth;
_inputAreaHeight = inputAreaHeight;
}
return self;
}
,你可能會這樣稱呼它:
InputManager *object = [[InputManager alloc] initWithAreaX: 20 AreaY: 20 AreaWidth: 25 AreaHeight: 25];
選項2
Objective-C++的全部目的是允許開發人員集成C++和Objective-C代碼。您想知道如何從Objective-C中調用Objective-C++方法,但Objective-C++的整個目的是整合這兩者,所以沒有必要試圖找到一個漏洞來調用Objective-C++方法。完全是Objective-C的文件。因此,第二個選擇是在Objective-C++文件中以「.mm」擴展名創建要調用Objective-C++方法的文件。
希望這會有所幫助!
用同樣的方法在C++ – borrrden 2012-07-26 05:54:37
你的意思是......會像 instanceOfInputManager - > _初始化(0,0,0,0) – 2012-07-26 05:59:08
,因爲不工作,要麼 – 2012-07-26 05:59:21