Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert between objc integers and C++ enums in Objective C++ code

I have an Objective C selector which returns integers. I have a C++ instance method which expects an enum. How can I link them? I'm in an Objective C++ (.mm) class while I'm doing this.

I want to call this:

TKClass::foo(MyEnum enumVal) { ... }

With the return value of this:

- (int) intValue { ... }

Like this:

myCPPInstance->foo([myObjCInstance intValue]);

I've tried casting (foo((MyEnum) [myObjCInstance integerValue])) but it doesn't work. I definitely don't want my Objective C object to know anything about the enum; intValue needs to stay as an integer. Similarly, I don't really want to have the C++ method worry about integer inputs when it should be accepting enums.

I'm not much of a C++ programmer, so sorry if this is easy.

Thanks

EDIT: The enum is defined like this:

enum MyEnum { 
   Apples = 0,
   Bananas = 1,
   Chocolate = 2
};
like image 911
Tim Avatar asked Jun 05 '26 14:06

Tim


1 Answers

It was a namespace issue. The cast should have been:

myCPPInstance->foo((myNamespace::MyEnum) [myObjCInstance intValue]);
like image 154
Tim Avatar answered Jun 07 '26 13:06

Tim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!