C++ functions in Objective C - c++

I am getting a bit confused how to call C++ functions in Objective C.
I've declared my functions as:
void InitializeSearchRegistration();
In the header file in between the interface and end. I've then defined it in my mm file as:
void InitializeServiceRegistration()
{
}
What I am struggling with is how to define it as public to ensure other classes can see it. I've tried public but get an error. Usually C++ methods have to be declared in a class block.
Any advice would be great.

You can't add C++ member functions to an Objective-C class using Objective-C++. You should consider redesigning your class to either have a totally C++ interface (in which case it should be a C++ class or struct) or a totally Objective-C interface (in which case it should define ObjC class and instance methods, not C++ member functions).

There are two ways to go about this, the first is to combine C++ and Objective C into another language, called Objective C++. The other is to communicate over plain C functions.

Related

Accesing inherited C++ library functions from C

I need to link my c-program with functions from inherited c++ library. The standard way is to use c++ wrappers to define a dummy c-structure that represents the c++ class. That works for simple (not inherited) classes. How can I modify it to work with inherited classes (i.e. air->cloud->lightning)?
Simple case:
typedef struct lightning lightning;

How to extend C++ classes from Terralang?

Is there a way to embed terralang into a C++ application, such that C++ classes can be extended from terra, including overloading virtual functions?
BONUS: Can this be done (semi?)automatically for a (large)set of classes, without needing to write "wrapper code" for each class and each virtual method?

Event handling between C++ and Objective C

I recently started working on an iOS game and decided to write a distinct part of the project in C++. This approach seems to work fine as long as the Objective C classes simply access some members or call functions on the C++ objects. However i can't seem to find an elegant way to make my Objective C classes respond to 'events' in the C++ classes. Any event handling system that uses callbacks seems out of the question (since Objective C methods and C++ functions and probably not interchangeable). All i can think of is using the delegate pattern and writing wrapper classes around my C++ delegate classes so i can use them in Objective C code. So my question is: Is there a better way of doing this?
NB:
I would like to prevent using Objective C directly in my C++ files, since these classes are supposed to be platform independent.
You may want to consider the mechanism that CoreVideo uses.
In their model, they have a mechanism which involves registering a C callback function( http://tinyurl.com/axtxajf ), and one of the parameters to this function is a void*, which can be typecast to the Objective-C class you need access to.
Here is an example of a C function you may implement in your C++ modules to register the callback function. The parameter are the callback function and the pointer to the class instance:
SetEventCallback(EventCallbackFunction, self);
When the event needs to be handled, the callback function is called, and you can typecast the void* to call Obj-C class and invoke the method:
void EventCallbackFunction(void* objCPtr)
{
[(MyObjCClass*)objCPtr someMethod];
}
You could use C++0x lambda functions, they are interchangeable (assignable to each other) with Objective-C blocks.

Calling Objective-C methods from CPP code

I am very new to Objective-C.
I wan to call Objective-C methods from a C++ class defined in separate .cpp file.
I have used the same mechanism described in this question's answer by dreamlax (not the PIMPL one)
http://stackoverflow.com/questions/1061005/calling-objective-c-method-from-c-method
But i am stuck at point how to call function int MyCPPClass::someMethod (void *objectiveCObject, void *aParameter); from CPP class with the parameters objectiveCObject.
Also can some one please explain how to call a C++ class functions from Objective-C Code with an Example.
You must use Objective-C++ in the file that does the call. To do so from Xcode, rename the file from Whatever.m to Whatever.mm. Then you can mix calls.

Calling an Objective-C class method from C++

I am having some trouble calling an objective-c class method from a C++ file. Example:
In my .h:
#interface MyClass : NSObject {
}
+ (void)myMethod:(NSString *)str;
In my .m:
+ (void) myMethod:(NSString *)str { ... }
In my .cpp:
??
How can you call myMethod since there is no class instance? Its essentially a static?
Thanks
Objects in C++ are incompatible with objects in Objective-C. You cannot simply call an Objective-C method from C++.
There are some solutions, however:
Use Objective-C++. Rename your .cpp to .mm, then you can use Objective-C syntax in your C++ code: [FlurryAnalytics myMethod: #"foo"];
Use direct calls to the Objective-C runtime system. I won't tell you how to, because I really think you shouldn't, and in fact that you don't want to.
Write a plain-C interface. That is, in some .m file, define void myFunction(const char *str) { ... } and call that from C++. You can find an example of this here.
You're going to need a .mm to call an Objective-C method, rather than a .cpp. Beyond that your comment that 'it's essentially static' is accurate in the sense that you would call it with similar logic to the way you would call static class functions in C++:
[MyClass myMethod:#"Some String"];
Though the method isn't static in the C++ sense for a bunch of reasons that don't affect this answer — it isn't resolved at compile time and it does operate within a class (the MyClass metaclass).