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

  • Home
  • SEARCH
  • 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 8844569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:31:15+00:00 2026-06-14T11:31:15+00:00

I have a question: I don’t explicitly new and delete my C++ instance, but

  • 0

I have a question:

I don’t explicitly “new” and “delete” my C++ instance, but it automatically calls “new” and “delete” when the objective-c class init. Any idea?

class myCppTestClass
{
public:
    myCppTestClass()
    {
        NSLog(@"MyCpp constructor");
    }

    ~myCppTestClass()
    {
        NSLog(@"MyCpp destructor");
    }
};



@interface MyTestClass : NSObject
{
    myCppTestClass myCppInstance;
}

@end


@implementation MyTestClass


@end

And I call it like this:

NSLog(@"Create an object.");
MyTestClass *objcObject = [[MyTestClass alloc] init];
NSLog(@"Object created");
[objcObject release];
objcObject = nil;
NSLog(@"Object released.");

Then I run it, the log is like this:

2012-11-16 12:01:18.747 iOSVersion[87248:f803] Create an object. 
2012-11-16 12:01:18.749 iOSVersion[87248:f803] MyCpp constructor
2012-11-16 12:01:18.750 iOSVersion[87248:f803] Object created
2012-11-16 12:01:18.751 iOSVersion[87248:f803] MyCpp destructor
2012-11-16 12:01:18.752 iOSVersion[87248:f803] Object released. 

It’s not about Automatic Reference Counnting, because it is closed, any ideas? Thank you so much.

Again, if MyTestClass is like this, the constructor and destructor will not been called:

@interface MyTestClass : NSObject
{
    myCppTestClass * myCppInstance;
}

and the log:

2012-11-16 12:22:38.710 iOSVersion[87428:f803] Create an object.
2012-11-16 12:22:38.711 iOSVersion[87428:f803] Object created
2012-11-16 12:22:38.712 iOSVersion[87428:f803] Object released.
  • 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-06-14T11:31:16+00:00Added an answer on June 14, 2026 at 11:31 am

    The language does not new and delete for you, but it calls the constructor and destructor. This is the standard behavior of C++: when you create an object, directly or indirectly, the constructor for the contained object gets called; similarly, when the containing object gets deallocated, the destructors of all contained instances get called as well.

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

Sidebar

Related Questions

I don't know if this question has been ask before. But I have a
This question is just for a sake of knowledge. I don't have any practical
Note:I don't mean some theoretical question which don't have any implementation just languages that
I have a question. I have a work this morning but i don't know
I'm new to Java so I have a simple question that I don't know
I have a question I tried to google it but looks like they don't
I have one question. I made quick search on the site, but don't found
So I know this question have been asked a lot , but I don't
I see this question have been asked in many different ways, but I don't
I have a question but I don't know how to search about it, so

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.