问题描述
C文件从.c
修改为到.m
文件,并且可以通过 其他Objective C文件(.m
文件)进行调用.
C files can be modified from .c
to .m
files and can be called from other Objective C files (.m
files).
C ++文件从.cpp
修改为到.mm
文件,也可以从其他 C ++文件(.mm
文件)进行调用.
C++ files can be modified from .cpp
to .mm
files and can be called from other Objective C++ files (.mm
files).
.m
个文件.但是 .mm
文件不能从.m
文件中调用.
.m
files can be called from .mm
files. But .mm
files cannot be called from .m
files.
在iPhone开发中,目标C ++ 是必需的编码,因为 UI 将在目标C中实现,而实现的任何其他协议都不能是目标C ++因为会从编写为目标C的用户界面调用此协议(已编写协议).
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 ++ 编码在什么场景中使用?
推荐答案
不确定您的意思是什么,但我认为这是错误的.
Not sure what you mean by this, but I think it's wrong.
Objective-C(++)的"Objective"部分在两种语言中都是相同的.无论是Objective-C还是Objective-C ++,这些对象都可以完全互操作.
The "Objective" part of Objective-C(++) is the same in both languages. It doesn't matter whether the implementation is Objective-C or Objective-C++, the objects will be fully interoperable.
重要的是在其中声明接口的头文件.例如:
What does matter is the header file in which the interface is declared. For instance:
@interface Foo
{
CPPFoo myFoo; // A C++ object
}
@end
不能包含在普通的Objective-C .m
文件中,因为C ++类在C语言中是非法的.解决此问题的一种方法是使用前向声明和指针,例如.
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 e.g.
#if defined __cplusplus
class CPPFoo;
#else
typedef struct CPPFoo CPPFoo;
#endif
@interface Foo
{
CPPFoo *myFoo; // NOTE: a pointer to a C++ object
}
@end
您需要在-init
中新建指针,然后在-dealloc/-finalize
You need to new the pointer in -init
and delete it in -dealloc/-finalize
不.我曾经认为(来自C ++背景)最好在任何地方都使用C ++,而仅在UI中使用Objective-C.但是,我很快就意识到,Objective-C的对象模型比C ++更好.因此,现在我仅在两种情况下考虑使用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. However, it didn't take long for me to realise that Objective-C's object model is better than that of C++. So now I would consider C++ in only two cases:
- 与使用C ++编写的库接口时
- 如果性能很重要并且您需要内置的对象模型(即您不想使用纯C)
这篇关于目标C和目标C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!