Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherit NSObject from C++ class

I'm emotionally attached to my C++ class, but I really want it to go into a tableview.

Is there a way to subclass NSObject from a standard c++ class?

this is what I tried to do:

class Model : NSObject {
public:
    NSString *key;
};

The compiler did not like it one bit.

It tells me the the base specifier must be a class.

Is there any kind of sneaky thing I can do?

Should I just make it a standard objective C++ object?

like image 899
Paul Wand Avatar asked Mar 16 '12 17:03

Paul Wand


People also ask

Can a struct inherit from NSObject?

A struct or enum can not conforms protocols that inheritance from class, AnyObject, marked as objc and NSObject. A class, struct or enum can conforms multiple protocols, you just need to add a comma after each protocol.

How do you subclass NSObject?

Create a new file and choose Cocoa Touch Class. Click Next and name the class “Person”, type “NSObject” for "Subclass of", then click Next and Create to create the file. NSObject is what's called a universal base class for all Cocoa Touch classes.

Can Objective-C class inherit from Swift class?

Unfortunately, that is the case. You cannot subclass a Swift class (even if it is a subclass of NSObject and available to the Objective-C runtime) because of deliberate limitations baked into Obj-C to block subclassing Swift classes in Obj-C code.

What is a NSObject Objective-C?

The root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.


2 Answers

You cannot mix inheritance between C++ and Objective-C classes.

Instead, just make a standard Objective-C++ object which wraps your C++ object. Teach the Obj-C object how to pass everything through to your C++ object.

like image 146
BJ Homer Avatar answered Sep 17 '22 13:09

BJ Homer


Nope, not possible.

C++ and Objective-C classes are implemented in very different ways. C++ uses a vtable for function lookup, and is static.

Objective-C, however, uses a dynamic dictionary-lookup, and methods can be added at runtime.

However, you could use object composition to do what you want:

// MyCPPClass.cpp
class MyCPPClass {
    int var;

public:
    void doSomething(int arg)
    {
        var += arg;
        std::cout << "var is: " << var << std::endl;
    }
};

// MyCPPWrapper.h
#ifndef __cplusplus
typedef void MyCPPClass;
#endif

@interface MyCPPWrapper : NSObject
{
    @public
    MyCPPClass *cppObject;
}

-(void) doSomething:(int) arg;

@end

// MyCPPWrapper.mm
@implementation MyCPPWrapper

-(void) doSomething:(int)arg
{
    if (cppObject)
        cppObject->doSomething(arg);
}

@end

// main.mm
int main(int argc, const char * argv[])
{
    @autoreleasepool {
        MyCPPWrapper *cppWrapper = [MyCPPWrapper new];
        cppWrapper->cppObject = new MyCPPClass();

        [cppWrapper doSomething:10];
        [cppWrapper doSomething:15];
    }

    return 0;
}
like image 21
Richard J. Ross III Avatar answered Sep 20 '22 13:09

Richard J. Ross III