0

I'm just learning Objective C (and objective-c++ for that matter) and I have an Objective-C++ class with the following constructor.

void InputManager::_init (int inputAreaX, int inputAreaY, int inputAreaWidth, int inputAreaHeight) 

How do I invoke this from objective C?

4
  • The same way you would in C++ Commented Jul 26, 2012 at 5:54
  • you mean...like instanceOfInputManager->_init(0, 0, 0, 0) Commented Jul 26, 2012 at 5:59
  • because that doesn't work either Commented Jul 26, 2012 at 5:59
  • Then you need to provide more info about what you did Commented Jul 26, 2012 at 6:01

2 Answers 2

2

This appears to be a pure C++ method so it would work exactly the same as in ordinary C++ (even in an Objective-C++ file). For instance you might have defined a variable on the stack:

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 

The name _init is a bit weird though; do you mean for this to be a constructor for the class? If so, InputManager::InputManager(int x, int y, int w, int h) should probably be defined instead.

If you actually want this class to be Objective-C only, the syntax and behavior are different.

Sign up to request clarification or add additional context in comments.

Comments

0

You have two options:

Option 1.

Translate it into Objective-C only code. I'm not so good with C++, but this might be what it looks like in the .h:

-(id)initWithAreaX: (int) inputAreaX AreaY: (int) inputAreaY AreaWidth: (int) inputAreaWidth AreaHeight: (int) inputAreaHeight; 

Since it looks like that's a constructor method, it would probably look like this in the implementation:

-(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; } 

And you might call it like this:

InputManager *object = [[InputManager alloc] initWithAreaX: 20 AreaY: 20 AreaWidth: 25 AreaHeight: 25]; 

Option 2.

The whole purpose of Objective-C++ is to allow the developer to integrate C++ and Objective-C code. You want to know how to call an Objective-C++ method from Objective-C, but the entire purpose of Objective-C++ is to integrate the two, so there's no point to trying to find a loophole to call an Objective-C++ method in a file that is otherwise completely Objective-C. So the second option is to just make the file that you want to call the Objective-C++ method in an Objective-C++ file with a ".mm" extension.

Hope this helps!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.