术语“不透明类型”是什么意思? 在“CFBundleRef 不透明类型”的上下文中意味着什么?

发布于 2024-07-16 15:11:23 字数 92 浏览 10 评论 0原文

有人对什么是“不透明类型”有很好的解释吗? 我在 CFBundleRef 的上下文中看到了这个术语,他们说:“CFBundleRef 不透明类型”。 这是只读类型吗?

Does someone have a good explanation of what an "opaque type" is? I saw that term in context of the CFBundleRef, where they were saying: "CFBundleRef opaque type". Is that a type that's readonly?

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

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

发布评论

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

评论(3

烟酉 2024-07-23 15:11:23

“不透明类型”是指您没有 structclass 的完整定义的类型。 在 C、C++ 和 Objective-C 中,您可以使用前向声明告诉编译器稍后将定义类型:

// forward declaration of struct in C, C++ and Objective-C
struct Foo;

// forward declaration of class in C++:
class Bar;

// forward declaration of class in Objective-C:
@class Baz;

编译器没有足够的信息让您直接使用 struct 执行任何操作> 或 class ,除了声明指向它的指针,但这通常就是您需要做的全部事情。 这允许库和框架创建者隐藏实现细节。 然后,库或框架的用户调用辅助函数来创建、操作和销毁前向声明的structclass 的实例。 例如,框架创建者可以为 struct Foo 创建这些函数:

struct Foo *createFoo(void);
void addNumberToFoo(struct Foo *foo, int number);
void destroyFoo(struct Foo *foo);

作为 Core Foundation 框架的一部分,Apple 制作了常见的 Objective-C 类,例如 NSStringNSArrayNSBundle 可供 C 程序员通过不透明类型使用。 C 程序员使用指针和辅助函数来创建、操作和销毁这些 Objective-C 类的实例。 苹果称之为“免费桥接”。 它们遵循一个通用的命名约定:“CF”前缀+类名+“Ref”后缀,其中“CF”代表“Core Foundation”,“Ref”是“Reference”的缩写,意思是它是一个指针。

An "opaque type" is a type where you don't have a full definition for the struct or class. In C, C++ and Objective-C, you can tell the compiler that a type will be defined later by using a forward declaration:

// forward declaration of struct in C, C++ and Objective-C
struct Foo;

// forward declaration of class in C++:
class Bar;

// forward declaration of class in Objective-C:
@class Baz;

The compiler doesn't have enough information to let you do anything directly with the struct or class except declare pointers to it, but this is frequently all you need to do. This allows library and framework creators to hide implementation details. Users of a library or framework then call helper functions to create, manipulate and destroy instances of a forward declared struct or class. For example, a framework creator could create these functions for struct Foo:

struct Foo *createFoo(void);
void addNumberToFoo(struct Foo *foo, int number);
void destroyFoo(struct Foo *foo);

As part of the Core Foundation framework, Apple makes common Objective-C classes like NSString, NSArray and NSBundle available to C programmers through opaque types. C programmers use pointers and helper functions to create, manipulate and destroy instances of these Objective-C classes. Apple calls this "toll-free bridging". They follow a common naming convention: "CF" prefix + class name + "Ref" suffix, where "CF" stands for "Core Foundation" and "Ref" is short for "Reference", meaning it's a pointer.

左秋 2024-07-23 15:11:23

不透明类型是一种“包装”较低级别类型的类型,通常在底层实现复杂或用户不需要了解内部工作原理时使用。 苹果在这里有一个关于不透明类型的好页面:

https://developer.apple.com/library/ios/#documentation/CoreFoundation/Conceptual/CFDesignConcepts/Articles/OpaqueTypes.html

例如,CFString 是一种不透明类型,因为它包装了一个字符数组,并保持其长度、其编码等,但并不直接允许用户访问这些值。 相反,它提供了访问或操作内部字段并向用户返回相关信息的方法。

An opaque type is a type that "wraps" lower-level types, and is often used when either the underlying implementation is complex, or the user simply does not need to know about the inner workings. Apple has a good page on opaque types here:

https://developer.apple.com/library/ios/#documentation/CoreFoundation/Conceptual/CFDesignConcepts/Articles/OpaqueTypes.html

For example, CFString is an opaque type because it wraps a character array, maintains its length, its encoding, etc., but does not directly allow the user to access these values. Rather it provides methods that access or manipulate internal fields and return to the user the relevant information.

清晰传感 2024-07-23 15:11:23

这是一个面向未来的结构。 例如:

typedef struct CFBundle *CFBundleRef;

如果没有“struct CFBundle”的实际定义,您的代码将无法访问 CFBundleRef 指针内的任何内容。 这是不透明的。

It's a future-declared structure. For example:

typedef struct CFBundle *CFBundleRef;

Without the actual definition of "struct CFBundle", your code cannot access anything within a CFBundleRef pointer. This is opaque.

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