Puoi mescolare C ++ con Objective-C se lo fai con attenzione. Ci sono alcuni avvertimenti, ma in generale possono essere mescolati. Se vuoi tenerli separati, puoi impostare una funzione wrapper C standard che fornisce all'oggetto Objective-C un'interfaccia in stile C utilizzabile da codice non Objective-C (scegli nomi migliori per i tuoi file, ho scelto questi nomi per verbosità):
MyObject-C-interface.h
#ifndef __MYOBJECT_C_INTERFACE_H__
#define __MYOBJECT_C_INTERFACE_H__
// This is the C "trampoline" function that will be used
// to invoke a specific Objective-C method FROM C++
int MyObjectDoSomethingWith (void *myObjectInstance, void *parameter);
#endif
MyObject.h
#import "MyObject-C-Interface.h"
// An Objective-C class that needs to be accessed from C++
@interface MyObject : NSObject
{
int someVar;
}
// The Objective-C member function you want to call from C++
- (int) doSomethingWith:(void *) aParameter;
@end
MyObject.mm
#import "MyObject.h"
@implementation MyObject
// C "trampoline" function to invoke Objective-C method
int MyObjectDoSomethingWith (void *self, void *aParameter)
{
// Call the Objective-C method using Objective-C syntax
return [(id) self doSomethingWith:aParameter];
}
- (int) doSomethingWith:(void *) aParameter
{
// The Objective-C function you wanted to call from C++.
// do work here..
return 21 ; // half of 42
}
@end
MyCPPClass.cpp
#include "MyCPPClass.h"
#include "MyObject-C-Interface.h"
int MyCPPClass::someMethod (void *objectiveCObject, void *aParameter)
{
// To invoke an Objective-C method from C++, use
// the C trampoline function
return MyObjectDoSomethingWith (objectiveCObject, aParameter);
}
La funzione wrapper non deve essere nello stesso .mfile della classe Objective-C, ma il file in cui esiste deve essere compilato come codice Objective-C . L'intestazione che dichiara la funzione wrapper deve essere inclusa sia nel codice CPP che nel codice Objective-C.
(NOTA: se al file di implementazione Objective-C viene assegnata l'estensione ".m", non si collegherà sotto Xcode. L'estensione ".mm" dice a Xcode di aspettarsi una combinazione di Objective-C e C ++, cioè Objective-C ++. )
È possibile implementare quanto sopra in modo orientato agli oggetti utilizzando l' idioma PIMPL . L'implementazione è solo leggermente diversa. In breve, inserisci le funzioni wrapper (dichiarate in "MyObject-C-Interface.h") all'interno di una classe con un puntatore void (privato) a un'istanza di MyClass.
MyObject-C-Interface.h (PIMPL)
#ifndef __MYOBJECT_C_INTERFACE_H__
#define __MYOBJECT_C_INTERFACE_H__
class MyClassImpl
{
public:
MyClassImpl ( void );
~MyClassImpl( void );
void init( void );
int doSomethingWith( void * aParameter );
void logMyMessage( char * aCStr );
private:
void * self;
};
#endif
Notare che i metodi wrapper non richiedono più il puntatore void a un'istanza di MyClass; ora è un membro privato di MyClassImpl. Il metodo init viene utilizzato per creare un'istanza di MyClass;
MyObject.h ( PIMPL )
#import "MyObject-C-Interface.h"
@interface MyObject : NSObject
{
int someVar;
}
- (int) doSomethingWith:(void *) aParameter;
- (void) logMyMessage:(char *) aCStr;
@end
MyObject.mm ( PIMPL )
#import "MyObject.h"
@implementation MyObject
MyClassImpl::MyClassImpl( void )
: self( NULL )
{ }
MyClassImpl::~MyClassImpl( void )
{
[(id)self dealloc];
}
void MyClassImpl::init( void )
{
self = [[MyObject alloc] init];
}
int MyClassImpl::doSomethingWith( void *aParameter )
{
return [(id)self doSomethingWith:aParameter];
}
void MyClassImpl::logMyMessage( char *aCStr )
{
[(id)self doLogMessage:aCStr];
}
- (int) doSomethingWith:(void *) aParameter
{
int result;
// ... some code to calculate the result
return result;
}
- (void) logMyMessage:(char *) aCStr
{
NSLog( aCStr );
}
@end
Si noti che MyClass viene istanziato con una chiamata a MyClassImpl :: init. Puoi istanziare MyClass nel costruttore di MyClassImpl, ma generalmente non è una buona idea. L'istanza di MyClass viene distrutta dal distruttore di MyClassImpl. Come con l'implementazione in stile C, i metodi wrapper rimandano semplicemente ai rispettivi metodi di MyClass.
MyCPPClass.h ( PIMPL )
#ifndef __MYCPP_CLASS_H__
#define __MYCPP_CLASS_H__
class MyClassImpl;
class MyCPPClass
{
enum { cANSWER_TO_LIFE_THE_UNIVERSE_AND_EVERYTHING = 42 };
public:
MyCPPClass ( void );
~MyCPPClass( void );
void init( void );
void doSomethingWithMyClass( void );
private:
MyClassImpl * _impl;
int _myValue;
};
#endif
MyCPPClass.cpp ( PIMPL )
#include "MyCPPClass.h"
#include "MyObject-C-Interface.h"
MyCPPClass::MyCPPClass( void )
: _impl ( NULL )
{ }
void MyCPPClass::init( void )
{
_impl = new MyClassImpl();
}
MyCPPClass::~MyCPPClass( void )
{
if ( _impl ) { delete _impl; _impl = NULL; }
}
void MyCPPClass::doSomethingWithMyClass( void )
{
int result = _impl->doSomethingWith( _myValue );
if ( result == cANSWER_TO_LIFE_THE_UNIVERSE_AND_EVERYTHING )
{
_impl->logMyMessage( "Hello, Arthur!" );
}
else
{
_impl->logMyMessage( "Don't worry." );
}
}
Ora accedi alle chiamate a MyClass tramite un'implementazione privata di MyClassImpl. Questo approccio può essere vantaggioso se stavi sviluppando un'applicazione portatile; potresti semplicemente sostituire l'implementazione di MyClass con una specifica per l'altra piattaforma ... ma onestamente, se questa è un'implementazione migliore è più una questione di gusti e bisogni.