目标C和目标C ++ [英] Objective C and Objective C++

查看:99
本文介绍了目标C和目标C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 ++ 编码在什么场景中使用?

推荐答案

.m文件可以从.mm文件调用.但是无法从.m文件中调用.mm文件.

.m files can be called from .mm files. But .mm files cannot be called from .m files.

不确定您的意思是什么,但我认为这是错误的.

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

iPhone开发中是否需要使用Objective C ++编码

Is Objective C++ coding necessary in iPhone development

不.我曾经认为(来自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 ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆