在iPhone上运行c++有什么限制?

时间:2022-09-01 23:51:37

I like C++ a lot and to be honest the Objective-C "super set" of C is more of a "super fail". Can an iPhone application be written in pure C++? Are there parts of the API that are unavailable from C++?

我很喜欢c++,老实说,C的Objective-C“超集”更像是一个“超级失败”。可以用纯c++编写iPhone应用程序吗?在c++中,API的某些部分是不可用的吗?

4 个解决方案

#1


5  

You can't code purely in C++. For one, the UIApplicationDelegate class every application needs to inherit is Objective-C.

不能仅仅用c++编写代码。首先,每个应用程序需要继承的UIApplicationDelegate类是Objective-C。

However, nothing is stopping you from coding everything that isn't framework related in Objective-C++. You'll still need to use the Objective-C calls for UIKit and other frameworks, but all of your application logic can be in C++.

然而,没有什么能阻止你编写所有与objective - c++无关的框架。您仍然需要对UIKit和其他框架使用Objective-C调用,但是所有的应用程序逻辑都可以使用c++。

From the Objective-C++ section of the Objective-C programming guide, these are the main limitations:

从Objective-C编程指南的objective - c++部分来看,这些是主要的限制:

Objective-C++ does not add C++ features to Objective-C classes, nor does it add Objective-C features to C++ classes. For example, you cannot use Objective-C syntax to call a C++ object, you cannot add constructors or destructors to an Objective-C object, and you cannot use the keywords this and self interchangeably. The class hierarchies are separate; a C++ class cannot inherit from an Objective-C class, and an Objective-C class cannot inherit from a C++ class. In addition, multi-language exception handling is not supported. That is, an exception thrown in Objective-C code cannot be caught in C++ code and, conversely, an exception thrown in C++ code cannot be caught in Objective-C code. For more information on exceptions in Objective-C, see “Exception Handling.”

objective - c++不向Objective-C类添加c++特性,也不向c++类添加Objective-C特性。例如,不能使用Objective-C语法调用c++对象,不能向Objective-C对象添加构造函数或析构函数,也不能将关键字this和self互换使用。类层次结构是分开的;c++类不能从Objective-C类继承,Objective-C类不能从c++类继承。此外,不支持多语言异常处理。也就是说,在Objective-C代码中抛出的异常不能在c++代码中被捕获,相反,在c++代码中抛出的异常不能在Objective-C代码中被捕获。有关Objective-C中的异常的更多信息,请参见“异常处理”。

#2


2  

Some Carbon APIs exist in the iPhone so you will be able to access them from within a purely c++ application. That being said, however, there are a lot of very important APIs that are Objective-C only (e.g., UIKit). With some good design decisions a reasonable C/C++ shim could be written to encapsulate the Objective-C necessary to get an iPhone application working. Once that's done you'd be able to write your app with traditional C++ no problem.

iPhone中存在一些Carbon api,因此您可以在纯c++应用程序中访问它们。尽管如此,仍然有许多非常重要的api是仅面向对象的- c(例如,UIKit)。有了一些好的设计决策,就可以编写一个合理的C/ c++ shim来封装使iPhone应用程序工作所必需的Objective-C。完成之后,您就可以用传统的c++编写应用程序了。

The way Objective-C++ is set up you can still write bona fide C++ and mix-in Objective-C calls where necessary. This has been the route I've taken in the iPhone applications I have developed.

Objective-C++的设置方式,您仍然可以在需要时编写真正的c++和mix-in Objective-C调用。这就是我开发的iPhone应用程序的路线。

#3


1  

I don't think that you'll manage to completely escape objective-c for any meaningful application, but you are free to code mostly in C++. In fact, sio2(one of the iphone game engines) is mostly C++ if you were to use that as the base for your application you could probably avoid writing objective-c yourself.

我不认为你会完全逃避objective-c对于任何有意义的应用程序,但是你可以在c++中*地编写代码。实际上,sio2(iphone游戏引擎之一)主要是c++,如果您将其作为应用程序的基础,您可能会避免编写objective-c。

I would like to know what you don't like about objective-c? I came from a C++ background and find Obj-C to be refreshing and more OO than C++.

我想知道你不喜欢objective-c的什么?我来自c++背景,发现object -C比c++更令人耳目一新,而且更多的是OO。

#4


1  

You only need to do the GUI in Obj-C (Obj-C++).

您只需要在object - c (obj-c++)中执行GUI。

There is no problem with integrating any other C++ code with the interface. Just be carefully to manually delete your pointers that are held inside Obj-C objects

与接口集成任何其他c++代码都没有问题。只需小心地手动删除保存在object - c对象中的指针

#1


5  

You can't code purely in C++. For one, the UIApplicationDelegate class every application needs to inherit is Objective-C.

不能仅仅用c++编写代码。首先,每个应用程序需要继承的UIApplicationDelegate类是Objective-C。

However, nothing is stopping you from coding everything that isn't framework related in Objective-C++. You'll still need to use the Objective-C calls for UIKit and other frameworks, but all of your application logic can be in C++.

然而,没有什么能阻止你编写所有与objective - c++无关的框架。您仍然需要对UIKit和其他框架使用Objective-C调用,但是所有的应用程序逻辑都可以使用c++。

From the Objective-C++ section of the Objective-C programming guide, these are the main limitations:

从Objective-C编程指南的objective - c++部分来看,这些是主要的限制:

Objective-C++ does not add C++ features to Objective-C classes, nor does it add Objective-C features to C++ classes. For example, you cannot use Objective-C syntax to call a C++ object, you cannot add constructors or destructors to an Objective-C object, and you cannot use the keywords this and self interchangeably. The class hierarchies are separate; a C++ class cannot inherit from an Objective-C class, and an Objective-C class cannot inherit from a C++ class. In addition, multi-language exception handling is not supported. That is, an exception thrown in Objective-C code cannot be caught in C++ code and, conversely, an exception thrown in C++ code cannot be caught in Objective-C code. For more information on exceptions in Objective-C, see “Exception Handling.”

objective - c++不向Objective-C类添加c++特性,也不向c++类添加Objective-C特性。例如,不能使用Objective-C语法调用c++对象,不能向Objective-C对象添加构造函数或析构函数,也不能将关键字this和self互换使用。类层次结构是分开的;c++类不能从Objective-C类继承,Objective-C类不能从c++类继承。此外,不支持多语言异常处理。也就是说,在Objective-C代码中抛出的异常不能在c++代码中被捕获,相反,在c++代码中抛出的异常不能在Objective-C代码中被捕获。有关Objective-C中的异常的更多信息,请参见“异常处理”。

#2


2  

Some Carbon APIs exist in the iPhone so you will be able to access them from within a purely c++ application. That being said, however, there are a lot of very important APIs that are Objective-C only (e.g., UIKit). With some good design decisions a reasonable C/C++ shim could be written to encapsulate the Objective-C necessary to get an iPhone application working. Once that's done you'd be able to write your app with traditional C++ no problem.

iPhone中存在一些Carbon api,因此您可以在纯c++应用程序中访问它们。尽管如此,仍然有许多非常重要的api是仅面向对象的- c(例如,UIKit)。有了一些好的设计决策,就可以编写一个合理的C/ c++ shim来封装使iPhone应用程序工作所必需的Objective-C。完成之后,您就可以用传统的c++编写应用程序了。

The way Objective-C++ is set up you can still write bona fide C++ and mix-in Objective-C calls where necessary. This has been the route I've taken in the iPhone applications I have developed.

Objective-C++的设置方式,您仍然可以在需要时编写真正的c++和mix-in Objective-C调用。这就是我开发的iPhone应用程序的路线。

#3


1  

I don't think that you'll manage to completely escape objective-c for any meaningful application, but you are free to code mostly in C++. In fact, sio2(one of the iphone game engines) is mostly C++ if you were to use that as the base for your application you could probably avoid writing objective-c yourself.

我不认为你会完全逃避objective-c对于任何有意义的应用程序,但是你可以在c++中*地编写代码。实际上,sio2(iphone游戏引擎之一)主要是c++,如果您将其作为应用程序的基础,您可能会避免编写objective-c。

I would like to know what you don't like about objective-c? I came from a C++ background and find Obj-C to be refreshing and more OO than C++.

我想知道你不喜欢objective-c的什么?我来自c++背景,发现object -C比c++更令人耳目一新,而且更多的是OO。

#4


1  

You only need to do the GUI in Obj-C (Obj-C++).

您只需要在object - c (obj-c++)中执行GUI。

There is no problem with integrating any other C++ code with the interface. Just be carefully to manually delete your pointers that are held inside Obj-C objects

与接口集成任何其他c++代码都没有问题。只需小心地手动删除保存在object - c对象中的指针