Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do c++ instances automatically call constructor and destructor when called via Objective-c classes?
    primarykey
    data
    text
    <p>I have a question:</p> <p>I don't explicitly "<code>new</code>" and "<code>delete</code>" my C++ instance, but it automatically calls "<code>new</code>" and "<code>delete</code>" when the objective-c class init. Any idea?</p> <pre><code>class myCppTestClass { public: myCppTestClass() { NSLog(@"MyCpp constructor"); } ~myCppTestClass() { NSLog(@"MyCpp destructor"); } }; @interface MyTestClass : NSObject { myCppTestClass myCppInstance; } @end @implementation MyTestClass @end </code></pre> <p>And I call it like this:</p> <pre><code>NSLog(@"Create an object."); MyTestClass *objcObject = [[MyTestClass alloc] init]; NSLog(@"Object created"); [objcObject release]; objcObject = nil; NSLog(@"Object released."); </code></pre> <p>Then I run it, the log is like this:</p> <pre><code>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. </code></pre> <p>It's not about Automatic Reference Counnting, because it is closed, any ideas? Thank you so much.</p> <p>Again, if MyTestClass is like this, the constructor and destructor will not been called:</p> <pre><code>@interface MyTestClass : NSObject { myCppTestClass * myCppInstance; } </code></pre> <p>and the log:</p> <pre><code>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. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload