1.16 Objective-C++
gcc
is at once a compiler for C,
Objective-C, and C++. You can intermix C++ and Objective-C code to
some degree. To instruct the compiler that a file contains C++ code
as well as Objective-C, use the file extension
.mm or .M instead of
.m.
Following are some ways in which C++ and Objective-C code can be used
together:
Objective-C objects can have fields that point to C++ objects, and
vice versa.
Objective-C code can call methods on C++ objects, and vice versa.
Objective-C objects can have C++ objects (as opposed to pointers) as
fields, but only if the C++ class has no virtual methods.
However, Objective-C and C++ are not completely compatible. Here are
some things you can't do:
Objective-C classes can't inherit from C++ classes,
and vice versa.
You can't declare Objective-C classes in C++
namespaces or templates, or vice versa.
You can't use C++ keywords for Objective-C variable
names.
You can't call Objective-C methods with C++ syntax,
or vice versa.
Finally, there are some restrictions that are imposed to avoid
ambiguity:
You can't use the name id as a
C++ template name. If you could, the declaration
id<TypeName>
var could be either a C++ template
declaration or an Objective-C declaration using a protocol.
If you are passing a globally-scoped C++ variable to an Objective-C
method, you need a space between the first and second colons. For
example:
[obj methodName: ::aGlobal].
|