문제

Are there any disadvantages (performance or otherwise) to using Objective C objects within a C++ class with Objective C++ files? For example, the difference between using Objective C types such as NSString directly like so:

class Hello {
private:
    NSString *text;

public:
    Hello() { this->text = @"Hello, world!";
    ~Hello();
    NSString* helloWorld() { return this->text; }
};

versus using standard lib C++ strings and wrapping them into NSStrings afterward. Is there any reason that directly using Objective C "types" is inferior? I've heard people recommend to keep Objective C and C++ as separate as possible and I'd like to hear the rational behind that. Either way I'll be hiding the C++ class behind a Objective C interface regardless to call it from .m files.

도움이 되었습니까?

해결책

I don't think there is any problem with performance, however it's not clear to me why you would want to encapsulate Objective-C objects within a C++ object.

One reason to keep C++ purely C++ is so it can interact with other C++ objects, which is no longer possible once you include Objective-C objects.

In order to allow a C++ object, with an embedded Objective-C object, to be used by another C++ class (where it needs to "see" the header file) I guess you'd have to use void * or something.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top