Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing C++ and Objective-C

I am using C++ as the app backbone and Objective-C for the GUI, that's fine.

But when it comes to mixing those code together in Objective-C++ (.mm file), I have got a few question:

1. Can I mix STL containers with Objective-C or Cocos2D objects?

E.g. In Objective-C header, can I do the following?

#include <vector>
#include <boost\shared_ptr.hpp>
@interface MyClass : NSObject {
  std::vector<boost::shared_ptr<CCSprite> > m_spriteList;
}

And then in the .mm file, I want to do

CCSprite* newSprite = [/* cocos2d stuff here... */];
m_spriteList.push_back(newSprite);

Is the above code valid? It certainly is in C++, but I am not sure when mixing C++ and Objective-C and Cocos2D.

2. Memory management using C++ smart pointer object in Objective-C?

When I try to use the C++ code in Objective-C, I want to declare a C++ object as a member variable in the Objective-C header file.

Say I have a C++ class declared in the test.h header:

Test{
};

In Objective-C header file, I want to do

#include "test.h"
#incude <boost/scoped_ptr.hpp>

#include <vector>
@interface MyClass : NSObject {
   Test* m_testObjectPtr; // (1)
   boost::scoped_ptr<Test>  m_testOjbSmartPtr; // (2)
}

In the above code, is (2) okay? Can I use smart pointers in Objective-C just like in C++ code? And can I assume the Test class destructor will be called when the MyClass object is destroyed?

Or if (2) is not okay in Objective-C++, is (1) okay? Would I need to manually call delete m_testObjectPtr in dealloc?

like image 799
RoundPi Avatar asked Nov 17 '11 18:11

RoundPi


People also ask

Is Objective-C as fast as C?

So, Objective-C will be just about as fast as calling the raw C functions yourself, with a little bit of overhead for looking up a function. @Goz that overhead is about 8-10 cycles on x86, and around 10-15 on ARM. So, a 'little' is correct. Someone benchmarked a recursive function with C and obj-C.

Why is Objective-C so different?

Objective-C uses the runtime code compilation Objective-C isn't a fast language. The main reason is that it uses the runtime code compilation, rather than the compile time. This means that when the Objective-C object calls for another object in the code, there is an extra level of indirection involved.

What is Objective-C mainly used for?

Objective-C is general-purpose language that is developed on top of C Programming language by adding features of Small Talk programming language making it an object-oriented language. It is primarily used in developing iOS and Mac OS X operating systems as well as its applications.


2 Answers

You can use smart pointer only on c++ classes. if you use then on objective-c classes you will either get compile error or crash somewhere.
You can also use containers with pointers of objective-c classes like

std::vector<CCSprite *> spriteList;

just make sure you retain them when you insert them to list and release them when you remove them.
In both cases, you can make a smart pointer of your own that calls retain and release in constructor/destruct/copy like needed and then don't worry about retain release.
Also destructor for member c++ objects will be called automatically when the object is deallocated.
An example of an objective c wrapper would be

template<typename T>
struct shared_objc_object
{
    T *Object;
    shared_objc_object : Object(nil) { }
    shared_objc_object(T *Object) : Object([Object retain]) { }
    shared_objc_object(shared_objc_object &other) :
        Object([other.Object retain]) { }
    ~shared_objc_object() { [Object release]; }
    shared_objc_object &operator =(shared_objc_object &other)
    {
        [Object release];
        Object = [other.Object retain];
    }
}

And you can use

std::vector<shared_objc_object<CCSprite *>> spriteList;
spriteList.push_back(some_sprite);

and don't care about retain/release

like image 157
Dani Avatar answered Sep 24 '22 01:09

Dani


There are some issues you'll want to be aware of. C++ classes do not enjoy the same scope based lifetime you might be used to when made into class members of Objective-C++ objects. When alloc/initing, the constructor won't be called, and when releasing, the destructor won't be called, unless you carefully use in place new/delete or hold on to a pointer and explicitly manage it with new/delete.

Also, if the Objective-C++ header needs to be shared with Objective-C files, you cannot use any C++ constructs at all. Both problems can be mitigated by hiding all C++ members using the pimpl pattern.

Can I mix STL containers with Objective-C or Cocos2D objects?

Yes, since Objective-C objects are just pointers to structs, you can store them easily in STL containers and even forward declare the type and pass it into pure C++ code. (Note, the C++ code can't really do much with the pointer without tricky and brittle code, but you can always pass the pointer back into Objective-C code later to get useful work done.)

Memory management using C++ smart pointer object in Objective-C?

You can use smart pointers to manage the lifetime of your Objective-C objects, but you will need to be careful that they do not call delete (the default behavior for most C++ smart pointers). With shared_ptr from C++11 or boost, you can provide a custom deleter; though now you have two reference counting systems. You can instead use boost::intrusive_ptr to skip that extra overhead and use Objective-C's reference counting directly.

like image 43
Marc Avatar answered Sep 24 '22 01:09

Marc