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
?