C ++ constructor call from Objective-C class

How can I call the C ++ constructor from the Objective-C class?

class CppClass { public: CppClass(int arg1, const std::string& arg2): _arg1(arg1), _arg2(arg2) { } // ... private: int _arg1; std::string _arg2; }; @interface ObjC: NSObject { CppClass _cppClass; } @end @implementation ObjC - (id)init { self = [super init]; if ( self ) { // what is the syntax to call CppClass::CppClass(5, "hello") on _cppClass? } return self; } @end 
+8
c ++ objective-c
source share
5 answers

When I end a situation where the default constructor does not cut it, I make the instance variable a pointer, and then use new in the init method and delete in the dealloc method.

This is a relatively recent thing that default constructors are generally called on for instance variables of Objective-C.

There is no Objective-C language specification, not to mention the Objective-C ++ extension specification. Apple has published a document called Objective-C Programming Language , but it almost never mentions C ++, so you often leave it alone when you need to clarify something that is not obvious. The Clang guys often know better, however.

+7
source share

If you need a stack-based C ++ class as the ivar of your obj-c class, you cannot pass any arguments to the constructor. The class will be created as part of your placement of the obj-c object. You can use the assignment operator in your -init , or you can change the inline object in some other way (for example, use member functions, etc.).

If the class absolutely needs to be built using arguments, then you cannot use the stack based object and instead allocate it on the heap with new (and then delete it with delete in -dealloc ) ..

+5
source share

Just for kicks - I found a partial solution using templates, but I doubt that you will get a lot of β€œreal” mileage with it ...

 template <int X, const char Y[]> class CPPClass { int _x; const char * _string ; public: CPPClass(); }; template <int X, const char Y[]> CPPClass<X, Y>::CPPClass() : _x(X) , _string(Y) { } extern const char kHelloWorld[] = "hello world"; @interface MyObject : NSObject { CPPClass<5, kHelloWorld> thing; } @end 

Note that the int template parameter can be a literal in place, but the const char[] template parameter must be declared in a variable with external binding .. (according to clang)

+1
source share

If you already use C ++ in your ObjC, you can make it a smart pointer, so you don’t have to worry about adding cleanup bits.

 #include <memory> @interface ObjC: NSObject { std::unique_ptr<CppClass> _cppClass; } @end @implementation ObjC - (id)init { self = [super init]; if ( self ) { _cppClass.reset(new CppClass(5, "hello")); } return self; } @end 
0
source share

I have not tried this, but for fun, I'm just going to riff on this:

At first it looks wrong:

 @interface ObjC: NSObject { CppClass _cppClass; } @end 

See that you are declaring a stack-based class in the obj c class interface.

Try

 CppClass *_cppClass; 

Then, while you include CppClass in your file

You have to since obj C interops with C and C ++ can create an instance with

 _cppClass = new CppClass(pass constructor args here 

I would not recommend putting a C ++ class in the same file

In the obj C project, you need a .mm file - not sure if CPP declarations are included. Do not do this.

So, you need a separate header and impl for your C ++ class, since usually you just include the CPP header and CPP code in your obj class as usual.

I could be here, haven't tried it, but obj C seems to gracefully call C and C ++ while you stick to the rules.

-one
source share

All Articles