[英]Objective C and Objective C++
C files can be modified from .c
to .m
files and can be called from other Objective C files ( .m
files). 可以将C文件从
.c
修改为.m
文件,并且可以从其他Objective C文件( .m
文件) 调用 。
C++ files can be modified from .cpp
to .mm
files and can be called from other Objective C++ files ( .mm
files). 可以将C ++文件从
.cpp
修改为.mm
文件,并且可以从其他Objective C ++文件( .mm
文件)中.mm
。
.m
files can be called from .mm
files . .m
文件可以从.mm
文件调用 。 But .mm
files cannot be called from .m
files . 但是
.mm
文件不能从.m
文件中调用 。
Is Objective C++ coding necessary in iPhone development as UI will be in Objective C and any other protocols implemented can't be Objective C++ as this (protocols written) will be called from the UI which is written Objective C . 是客观的C ++编码iPhone开发必要的UI将在目标C和实施的任何其他协议不能客观C ++,因为这(书面协议)将被从被写入目标C的UI叫 。
In what scenario is this Objective C++ coding used? 在什么情况下使用此Objective C ++编码?
Objective-C++ is used whenever you want to mix Objective-C code and C++ code. 每当您要混合使用Objective-C代码和C ++代码时,都会使用Objective-C ++。
Your statement that ".mm files cannot be called from .m files" is not true. 您说“无法从.m文件调用.mm文件”的说法是错误的。 If you put C++ in the header then you can't call it from a pure Objective-C file, but you can have a purely Objective-C interface for a class that has an implementation that uses C++.
如果将C ++ 放在标头中,则不能从纯Objective-C文件中调用它,但是对于具有使用C ++的实现的类,您可以具有纯Objective-C接口。 A common example is wrapping an existing C++ class (perhaps some existing library) as an Objective-C class.
一个常见的示例是将现有的C ++类(也许是一些现有的库)包装为Objective-C类。
.m files can be called from .mm files.
.m文件可以从.mm文件中调用。 But .mm files cannot be called from .m files.
但是.mm文件不能从.m文件中调用。
Not sure what you mean by this, but I think it's wrong. 不知道您的意思是什么,但我认为这是错误的。
The "Objective" part of Objective-C(++) is the same in both languages. 在两种语言中,Objective-C(++)的“ Objective”部分均相同。 It doesn't matter whether the implementation is Objective-C or Objective-C++, the objects will be fully interoperable.
无论实现是Objective-C还是Objective-C ++,对象都可以完全互操作。
What does matter is the header file in which the interface is declared. 重要的是声明接口的头文件。 For instance:
例如:
@interface Foo
{
CPPFoo myFoo; // A C++ object
}
@end
can't be included in a normal Objective-C .m
file because C++ classes are illegal in C. One way to get around this is to use forward declarations and pointers eg 不能包含在普通的Objective-C
.m
文件中,因为C ++类在C中是非法的。解决此问题的一种方法是使用前向声明和指针,例如
#if defined __cplusplus
class CPPFoo;
#else
typedef struct CPPFoo CPPFoo;
#endif
@interface Foo
{
CPPFoo *myFoo; // NOTE: a pointer to a C++ object
}
@end
You need to new the pointer in -init
and delete it in -dealloc/-finalize
您需要在
-init
新建指针,并在-dealloc/-finalize
删除它
Is Objective C++ coding necessary in iPhone development
在iPhone开发中是否需要Objective C ++编码
No. I used to think (coming from a C++ background) that it would be best to use C++ everywhere and Objective-C only in the UI. 不。我曾经认为(来自C ++背景)最好在各处使用C ++,而仅在UI中使用Objective-C。 However, it didn't take long for me to realise that Objective-C's object model is better than that of C++.
但是,我很快就意识到,Objective-C的对象模型比C ++更好。 So now I would consider C++ in only two cases:
所以现在我仅在两种情况下考虑使用C ++:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.