How to call C ++ method from Objective-C Cocoa Interface using Objective-C ++

Currently, my OS X 10.9 project is configured with a graphical interface in Objective-C and all processing in the C ++ class. This does not seem to be the best way, but it has been given to me, and I have to work with these restrictions. Cocoa's GUI currently has NSSlider . I want this NSSlider control the variable x in a C ++ class.

Currently, the tolerance panel works when I run the program (the working sense of it is constantly updating its value in my rolling manner).

NSSlider Header ( Slider.h ):

 @interface Slider : NSObject { @private __weak IBOutlet NSTextField *sliderValue; __weak IBOutlet NSSlider *slider; } - (IBAction)updateSliderValue:(id)sender; @end 

Implementation for NSSlider ( Slider.mm ):

 #import "Slider.h" // The NSSlider object #import "CMain.h" // The C++ class that does all the processing (not sure if this is the way to include a C++ interface in an Objective-C++ class or if this will allow me to call its methods) @implementation Slider -(void) awakeFromNib { [slider setIntValue:40]; [sliderValue setIntValue:[slider intValue]]; } - (IBAction)updateSliderValue:(id)sender { [sliderValue setIntValue:[slider intValue]]; // Here is where I'd think that if I have a setValueofX(int number) method in the C++ class // I could call this somehow using objective-C++. I don't know if I can or how to call this method here } 

Here is the corresponding snippet from Main.h :

 class CMain(){ public: CMain(); void setValueofX(int number); int getValueofX(); private: int x; }; 

How to include CMain.h in Objective-C++ (which, .h file or .mm file? And how?) So that it can call methods in CMain.h ? How would I formulate a method call in Objective-C to set the value of x using setValueofX(sliderValue) ?

Please let me know if you need more information! Any help or thoughts appreciated!

Thanks!

0
source share
3 answers

I found a solution, noting that there was an existing CWrapper.h wrapper CWrapper.h , and that was the objective-C ++ implementation class CWrapper.mm . There was a CMain variable created as CMain . I made a getter method for x , and I just created a static method in the wrapper class + (void) passX:(int) number; which I called in the slider class. I chose the static method because for this application this value should never be different between objects. I hope I made the right choice here!

See the following code changes:

I added this to the Slider.h file.

 - (int)X; 

I added a getter and [CWrapper passX:[self getX]]; to the updateSliderValue method in the updateSliderValue file.

 - (int)X { return [slider intValue]; } - (IBAction)updateSliderValue:(id)sender { [sliderValue setIntValue:[slider intValue]]; [CWrapper passX:[self getX]]; } 

This is the code I added to CWrapper.h.

 + (void) passX:(int) number; 

This is the code that I added to CWrapper.mm.

 + (void) passX:(int)num { cmain.setValueofX(num); } 
+1
source

The code for calling the setter is the same as in C ++. However, you will need a pointer to a CMain object to call a method on it. I do not know where this object is located. If the object does not already exist in another object, you probably want to create it, and you can simply declare an instance variable CMain myCMain; and then call myCMain.setValueOfX( [slider intValue] ); in updateSliderValue:

As for where to include the C ++ class, this is really your choice. However, if you use any C ++ in your header, you will need a ton of thorough additional work to include it from simple .m files. Therefore, in general, I try to stick with simple C and ObjC in the header and use C ++ only in the .mm file. You can use the class extension (sometimes also called class extension) to declare additional ivars in your .mm file so that they do not fall into the header.

If you want more information about ObjC ++, I answered in detail another message: Can I separate the main function and C ++ classes from Objective-C and / or C-routines when compiling and linking? , as well as links to Podcast, in which I was a guest, where I talk about many details and tricks for integrating ObjC and C ++.

Also: I hope these are not the real names and comments you are using. Never have an ObjC class without a prefix (3 letters, Apple stated that it reserves all 2-letter prefixes for its own use, and in the past they used internal class names that broke some programs for people as a prefix). In addition, β€œSlider” is a bad name, because NSSlider is a real slider, and it sounds like it needs to be a subclass. You really want to call it ISTSliderController or something else.

+3
source

Here are all the answers of objective-c and objective-c ++:

CMain.h:

 #ifndef Testing_CMain_h #define Testing_CMain_h @interface CCMain : NSObject -(CCMain*) init; -(void) dealloc; -(void)setValueofX:(int)number; -(int)getValueofX; @end #endif 

CMain.mm:

 #import <Foundation/Foundation.h> #import "CMain.h" class CMain { private: int x; public: CMain() : x(0) {} void setValueofX(int number) {x = number;} int getValueofX() const {return x;} }; @interface CCMain () @property (nonatomic, assign) CMain* inst; @end @implementation CCMain -(CCMain*) init { if (!_inst) { _inst = new CMain(); } return self; } -(void) dealloc { delete _inst; _inst = nil; } -(void)setValueofX:(int)number { _inst->setValueofX(number); } -(int)getValueofX { return _inst->getValueofX(); } @end 

and if you want to use C style functions, then:

 setValueofX(cmain_instance, value); int val = getValueofX(cmain_instance); 

And this works because the function of the C ++ class in C is the same as:

 void CMain::MyFunc(int X); //vs.. void MyFunc(CMain* inst, int X); 

Both are the same.

-2
source

Source: https://habr.com/ru/post/1213212/


All Articles