Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C++ C++ object destructor

I have an Objective-C class which one of it's variables is an C++ object (most of my code is C++, but I need some ObjC classes to integrate with iOS libraries). Does Objective-C++ guarantees that the C++ object will be correctly destroyed when the Objective-C object is destroyed?

Some example code:

class MyCppClass {
    // ...
};

@interface MyObjCClass : NSObject {
    MyCppClass myCppObject; // is it ok to do it?
}

// ...

@end
like image 992
fbafelipe Avatar asked Jul 30 '12 16:07

fbafelipe


People also ask

Does Objective C have constructors and destructors?

Traditionally in languages like Java and C# a constructor is where you would perform your initializations. In Objective-C you would do so in the init method even though you create a convenience constructor.

What is difference between constructor and destructor?

The constructor initializes the class and allots the memory to an object. If the object is no longer required, then destructors demolish the objects.

Can destructor be overloaded?

The destructor is only one way to destroy the object create by constructor. Hence destructor can-not be overloaded. Destructor neither requires any argument nor returns any value. It is automatically called when object goes out of scope.

What is destructor in OOP?

A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete . A destructor has the same name as the class, preceded by a tilde ( ~ ). For example, the destructor for class String is declared: ~String() .


1 Answers

Yes. After the -dealloc method is called, a hidden .cxx_destruct method is called. This method calls all the destructors of all instance variables that have a destructor.

like image 199
chmeee Avatar answered Oct 13 '22 10:10

chmeee