关于结构的基本问题

发布于 2024-11-07 13:40:07 字数 928 浏览 0 评论 0原文

struct
{
int integer;
float real;
}
first_structure;

因此,我们可以通过编写来引用first_struct的成员

first_structure.integer = 7

如果我写:

    struct two_numbers
    {
    int integer;
    float real;
    }
    first_structure;

那么我可以使用标签*two_numbers*来创建第二个结构,如下所示:

struct two_numbers second_structure;

我也明白typedef可以用于创建同义词。

但我无法理解下面的代码(来自页面 http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html):

typedef struct objc_class *Class;
typedef struct objc_object {
    Class isa;
} *id;

每个对象都有一个 isa 变量来告诉它它是哪个类的实例。

它怎么知道???请指导我理解这段代码的含义。

谢谢。

struct
{
int integer;
float real;
}
first_structure;

So we can refer to a member of the first_structure by writing

first_structure.integer = 7

If I write:

    struct two_numbers
    {
    int integer;
    float real;
    }
    first_structure;

then I can use the tag *two_numbers* to create a second structure like this:

struct two_numbers second_structure;

I also understand that typedef can be used to create synonyms.

But I am unable to understand the code below (from the page http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html):

typedef struct objc_class *Class;
typedef struct objc_object {
    Class isa;
} *id;

Every object thus has an isa variable that tells it of what class it is an instance.

HOW can it tell???? Please guide me through the meaning of this code.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琴流音 2024-11-14 13:40:07

这是一个快捷方式:

struct objc_object
{
    Class isa;
};
typedef struct objc_object * id

id 类型是指向 objc_object 结构的指针。
因此,当使用 id 类型时,您将使用 -> 运算符而不是 .,因为它是一个指针。

请注意,Class 类型也是指向结构的指针。
这就是所谓的不透明类型。

它基本上意味着编译器将能够计算大小,因为我们只有一个指针,但它不知道实现细节。

这种模式用于隐藏结构的实际实现。
类型在头文件中定义,但实现仅在源文件中定义。

例如,在头文件中:

typedef struct A_Struct * A;

这是有效的,它定义了结构指针的类型,即使该结构实际​​上未知。

然后在源文件中:

struct A
{
    int x;
    int y;
};

这是真正的实现。用户将能够创建 A 类型对象,但无法访问成员,因为它们仅在源文件中可见。
用户将向您传递 A 类型对象,但您会将它们转换为 struct A,以便您可以访问它的成员。

IE:

void foo( A someObject )
{
    struct A * a;

    a    = ( struct A * )someObject;
    a->x = 42;
}

That's a shortcut to:

struct objc_object
{
    Class isa;
};
typedef struct objc_object * id

The id type is a pointer to an objc_object structure.
So when using an id type, you will use the -> operator instead of ., since it's a pointer.

Note that the Class type is also a pointer to a structure.
That's called an opaque type.

It basically means the compiler will be able to calculate the size, since we have only a pointer, but it won't know the implementation details.

That kind of pattern is used to hide the actual implementation of a structure.
The type is defined in a header file, but the implementation is only defined in a source file.

For instance, in a header file:

typedef struct A_Struct * A;

That's valid, and it defines a type to a structure pointer, even if that structure is not known actually.

Then in a source file:

struct A
{
    int x;
    int y;
};

Here's the real implementation. Users will be able to create A typed objects, but won't be able to access the members, since they are known only in your source file.
Users will pass you A typed objects, but you'll be cast them to a struct A, so you can access it members.

IE:

void foo( A someObject )
{
    struct A * a;

    a    = ( struct A * )someObject;
    a->x = 42;
}
独木成林 2024-11-14 13:40:07

您可以看出,因为当实例化对象时,运行时会设置 isa 指针来反映类的名称。类似于 Objective-C 中“选择器”的使用方式 - 如果您不熟悉,基本上选择器是一个方法的名称。在幕后它只是一个 NSString,但它不一定是(替代实现 Cocotron 使用我相信的整数类型的枚举)。

基本上,运行时能够知道正在创建什么对象,因此可以设置 isa 指针。内省可以发挥作用,因为如果您将对象与“类”进行比较,您可能只是在幕后比较 NSString。

我希望这能解决您的疑虑,如果没有,我可以帮助您澄清

You can tell because when an object is instantiated, the runtime sets the isa pointer to reflect the name of the class. Similar to how a "selector" in objective-c may be used - if you're not familiar, basically a selector is a name of a method. Behind the scenes it is just an NSString, but it doesn't have to be (alternate implementation The Cocotron uses an enumeration of integer type I believe).

Basically the runtime is able to know what object is being created and therefore can set the isa pointer. And introspection can work because if you compare what the object is against a 'Class' you're probably just comparing behind-the-scenes NSStrings.

I hope this addresses your concern, if not I can help clarify

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文