Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6820877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:28:40+00:00 2026-05-26T21:28:40+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:28:40+00:00Added an answer on May 26, 2026 at 9:28 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building an app using Brunch and Backbone.js that is to include nested menus.
I'm in the process of creating a Backbone.js app using Require.js. Each view file
I'm using Backbone.js in a Rails app and I need to do file uploads
How do I set IncludeExceptionDetailInFaults in code without using App.Config?
I'm using Google App Engine and Django templates. I have a table that I
I am using backbone to build my web app. Currently I am facing an
I'm trying to keep track of the state in this app using Backbone.js: I
I'm currently using Handlebars.js (associated with Backbone and jQuery) to make a web app
I am using fetch in the index action of the following backbone.js controller: App.Controllers.PlanMembers
I am building a js heavy app using backbone.js. One problem I've run into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.