简体   繁体   English

目标C和目标C ++

[英]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 ++:

  • when interfacing to libraries that were written in C++ 与以C ++编写的库接口时
  • if performance is important and you need an built in Object model (ie you don't want to use pure C) 如果性能很重要并且您需要内置的对象模型(即您不想使用纯C)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM