Search code examples
c++objective-cobjective-c++growl

Objective C++ call Objective C


I have a growl action I need to call from Objective C++

- (NSDictionary *) registrationDictionaryForGrowl {
    return [NSDictionary dictionaryWithObjectsAndKeys:
            [NSArray arrayWithObject:@"Alert"], GROWL_NOTIFICATIONS_ALL,
            [NSArray arrayWithObject:@"Alert"], GROWL_NOTIFICATIONS_DEFAULT
            , nil];
}

is the action in Objective C, but I need to put it in a .mm file (Objective C++) and I'm having a hard time trying to convert it properly because I need to put it inside a (C++) function inside a (C++) class.

Any idea how I could transfer this over to Objective C++?


Solution

  • From what I gather you want to provide a C++ object to Growl, which expects and Objective-C object. You can't do this.

    Objective-C is a set of language extensions welded onto C. Objective-C++ is the result of welding exactly the same extensions onto C++. So in Objective-C++ you have both Objective-C style objects and C++ objects, and they are completely different things. One cannot be used when the other is expected. They have different ABIs, different schemes for resolving a method call/message send, different lifetime management, etc.

    The purpose of Objective-C++ is not to make Objective-C objects and C++ objects compatible, but simply to make Objective-C code usable from C++ and C++ code usable from Objective-C++.

    So what you could do is create your C++ class that you want to use as a delegate, and then create an Objective-C++ class that wraps your C++ class and has methods that simply forward to an instance of your C++ class.

    class Foo {
        int bar(int i) { return i+1; }
    };
    
    @interface ObjCFoo : NSObject
    {
        Foo foo;
    }
    
    - (int) bar:(int) i;
    @end
    
    @implementation ObjFoo
    - (int) bar:(int) i;
    {
        return foo.bar(i);
    }
    @end