为什么一个对象变量必须是一个指针? [英] Why does an object variable have to be a pointer?

查看:219
本文介绍了为什么一个对象变量必须是一个指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我声明一个 NSMutableString

NSMutableString *str_value;

code>)?

推荐答案

方案

回想一下Objective C是C的超集。当你在C中声明一个不含 * 的变量时,该变量的存储器如果是局部变量则被分配在自动存储器中,如果它是结构的成员则被指定为其外部结构的一部分,或者如果它是静态或全局的,则被指定在静态存储器中。在函数的参数列表中使用不带 * 的类型名或结构标记表示传递的值。

Recall that Objective C is a superset of C. When you declare a variable without * in C, it is an indication that the memory for that variable is allocated either in the automatic storage if it is a local variable, as part of its outer structure if it is a member of a structure, or in the static memory if it is a static or a global. Using the typename or a structure tag without * in a parameter list of a function indicates passing by value.

Objective C语言的设计者可能采取类似Java的路由,使每个类实例是一个没有指针语法的指针,但是Objective C中的程序的读者需要知道一个名字是否代表一个typedef基于一个 struct id 类型来回答甚至关于该类型的对象的最基本的问题,例如如果它被隐式传递通过指针或值,如果它被分配作为拥有结构的一部分或作为结构内的指针指向的堆对象,等等。

The designers of the Objective C language could have taken the Java-like route, making every class instance is a pointer without the pointer syntax, but then the readers of programs in Objective C would need to know if a name represents a typedef based on a struct or an id type to answer even the most basic questions about objects of that type, such as if it is implicitly passed by pointer or by value, if it is allocated as part of the owning structure or as a heap object pointed to by a pointer inside the structure, and so on.

避免这种混淆,Objective C的设计者决定保留 id 对象的显式指针语法。

To avoid this kind of confusion, designers of Objective C decided to preserve the explicit pointer syntax for id objects.

这篇关于为什么一个对象变量必须是一个指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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