c++ - new in Objective-C in init - how to handle std::bad_alloc? -
i need wrap c++ object inside objective-c class - did putting c++ objects inside @implementation
in .mm
file this:
myobjcclass.h:
@interface myobjcclass : nsobject @end
myobjcclass.mm:
@implementation myobjcclass { mycppclass member; } @end
sadly had give since code used in project targets 32-bit architectures , feature not supported. did forward struct declaration, struct defined in .mm
file , wraps c++ object:
myobjcclass.h:
struct myobjcclassprivate; @interface myobjcclass : nsobject { myobjcclassprivate* memberprivate; } -(void)dealloc; -(id)init; @end
myobjcclass.mm:
struct myobjcclassprivate { mycppclass member; } @implementation myobjcclass -(void)dealloc { delete memberprivate; memberprivate = nullptr; } -(id)init { if (self = [super init]) { memberprivate = new myobjcclassprivate; // happens if new throws std::bad_alloc ? } return self; } @end
the question - should care if allocation new fails? consequences (apart exception being thrown?) - need additional cleanup ojbective-c class instance?
i dont want put c++ stuff in header since class used plain objective-c code, fails compile.
Comments
Post a Comment