开发者

Mixing Objective-C and C++

I'm trying to mix Objective-C with C++. When I compile the code, I get several errors.

A.h

#import <Cocoa/Cocoa.h>
#include "B.h"

@interface A : NSView {
    B *b;
}

-(void) setB: (B *) theB;

@end

A.m

#import "A.h"

@implementation A

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    // Drawing code here.
}

-开发者_JAVA技巧(void) setB: (B *) theB {
    b = theB;
}

@end

B.h

#include <iostream>

class B {

    B() {
        std::cout << "Hello from C++";
    }

};

Here are the errors:

/Users/helixed/Desktop/Example/B.h:1:0 /Users/helixed/Desktop/Example/B.h:1:20: error: iostream: No such file or directory
/Users/helixed/Desktop/Example/B.h:3:0 /Users/helixed/Desktop/Example/B.h:3: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'B'
/Users/helixed/Desktop/Example/A.h:5:0 /Users/helixed/Desktop/Example/A.h:5: error: expected specifier-qualifier-list before 'B'
/Users/helixed/Desktop/Example/A.h:8:0 /Users/helixed/Desktop/Example/A.h:8: error: expected ')' before 'B'
/Users/helixed/Desktop/Example/A.m:26:0 /Users/helixed/Desktop/Example/A.m:26: error: expected ')' before 'B'
/Users/helixed/Desktop/Example/A.m:27:0 /Users/helixed/Desktop/Example/A.m:27: error: 'b' undeclared (first use in this function)


You need to name your .m files .mm. And you will be able to compile C++ code with Objective-C.

So, following your example, your AView.m file should be named AView.mm. It's simple as that. It works very well. I use a lot of std containers (std::vector, std::queue, etc) and legacy C++ code in iPhone projects without any complications.


Never mind, I feel stupid. All you have to do is rename AView.m to AView.mm so the compiler knows it's Objective-C++, and it compiles without a problem.


you could keep the interface cleaner with forward declaration of C++ classes:

#import <AnObjCclass.h>
class DBManager; // This is a C++ class. NOTE: not @class

@interface AppDelegate : UIResponder <UIApplicationDelegate,
                                    CLLocationManagerDelegate,
                                    MFMailComposeViewControllerDelegate>
{
    DBManager* db;
...
}


I am sharing some of the points that I understood on this topic.

We can mix both .cpp and .m files with a pure C interface. As we know the Clang compiler will support C++, Objective C as well as Objective C++, it might be a better means for mixing these languages.

One thing when mixing these languages to be taken care is using the header files. We can keep the C++ out of our Objective C headers by declaring the Cpp objects in class extensions.

Alternatively we can declare the cpp objects just at the start of @implementation block in our Objective Cpp(.mm) file.

Managing the memory will be a concern when we are dealing with Cpp objects. We can allocate memmory for an object using ‘new’ and release the memory by calling ‘delete object’. Normally if we are using ARC, we need not be aware of releasing the memory for an object.

While using the cpp classes, we can declare a Cpp object in two ways say CppWrapper wrapper and CppWrapper *wrapper where CppWrapper is a Cpp class. When we are using the latter the programmer is responsible for managing the memmory.

Another main thing is that when we are calling an objective C method with parameters, we are passing the references, while in cpp we need to pass the parameters by reference by using the ‘&’ keyword, otherwise copy of the object is been passed.

Deallocation of Objective C object is handled at runtime, where when ‘delete’ is invoked to an Cpp object, it will no longer remains in the memory.

When writing Cpp, we have shared pointer and weak pointers which is similar to the strong and weak in Objective C.

http://philjordan.eu/article/mixing-objective-c-c++-and-objective-c++ http://www.raywenderlich.com/62989/introduction-c-ios-developers-part-1


In situations where you wish to introduce a simple C++ function like std::cout << then Hot Licks offers a good alternative.

Change the "Identity and Type" From: Objective-C source To: Objective-C++ source

The .mm extension simply identifies the file type; and then you are seeking a Objective-C++ not a Objective-C type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜