Calling Objective-C methods from CPP code - c++

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.

Related

How to send a message from a C++ class to Objective-C ViewController?

I want to send a message including two parameters from a C++ class(.cpp file) to a Object-C ViewController(.m file), how can I do that?
As molbdnilo commented, change your C++ implementation file to extension .mm, making Xcode build it as Objective-C++. While you're at it, make the view controller implementation file .mm too. That way you can use both C++ and Objective-C types and syntax in both. You can either make an Objective-C method call from a C++ function, or handle a C/++ function in the view controller implementation module (but not the Obj-c class itself):
Try: 1. You can include Objective-C method syntax (square brackets and all) inside any C++ function implementations in a .mm file. Convert the params to C primitive types or Obj-C class instances to pass them.
Or: 2. Make your view controller implementation file a .mm, and write a global C or C++ function in it that you can declare extern in your C++ file and call from there. This removes the need to pass the view controller pointer to the C++ module. What I usually do here is keep a static pointer in the VC implementation file for the global function to access the view controller. Of course, that only gets you one instance. For multiple instances of the controller, you'll need another approach, such as #1.

C++ functions in Objective 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.

move class function realization from .h to .cpp procedure

ATL project "Class>Add>Implement Interface" procedure has generated a lot of class functions from IDL to header file. As I understood it would be more clever to have function realization in cpp file. Is it possible to move realization to cpp file somehow automatically?
I believe that many ATL classes are template classes, and the source code for templates needs to be available at each point where those classes are used (with the usual exceptions where a declaation might suffice...)
So moving the code into a separate .cpp file wouldn't work.

Why we need to copy-and-paste functions declaration into inherited class header

This is for C++.
Usually we have our function declaration in header file and definition in source file.
Say we have a class A with some functions:
//< A.hpp
class A
{
public:
virtual funcA();
virtual funcB();
}
And we want to have a class inherit from A and override its functions.
//< childA.hpp
class childA
{
virtual funcA();
virtual funcB();
}
Everytime we change the declarations of funcA() funcB(), we need to copy-and-paste the new declarations to the child classes header files. If the inheritance chain is long, it's quite bother.
I remember we don't have this problem with Object-C, do we?
You don't need to copy a member function declaration to the child class's header file unless you want to override it. In that case, I believe the main reason you're required to declare it is to inform anyone reading your header file that the child class is providing a different implementation. In principle, the compiler can figure it out automatically, but it could be a real pain for a human to do the same thing manually.
Note that in many cases, people reading your header files may not have access to the actual source code for the body (e.g., if it's a proprietary library that is delivered to them as compiled objects), so they can't just go look at the body to figure it out.
From the Objective-C article on Wikipedia:
Objective-C, like Smalltalk, can use dynamic typing: an object can be sent a message that is not specified in its interface.
http://en.wikipedia.org/wiki/Type_system#Dynamic_typing
C++, on the other hand, is statically typed. It's a stricter compile-time restraint.

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).