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?
有人对“不透明类型”有很好的解释吗?我在CFBundleRef的上下文中看到了这个术语,他们说:“CFBundleRef不透明类型”。那是只读类型吗?
3 个解决方案
#1
68
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:
“不透明类型”是指结构体或类没有完整定义的类型。在C、c++和Objective-C中,您可以通过使用forward声明告诉编译器稍后将定义一个类型:
// 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
:
编译器除了声明指向它的指针之外,没有足够的信息可以让您直接对结构体或类执行任何操作,但这通常是您需要做的全部工作。这允许库和框架创建者隐藏实现细节。库或框架的用户然后调用helper函数来创建、操作和销毁前声明的结构体或类的实例。例如,框架创建者可以为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.
作为核心基础框架的一部分,Apple通过不透明的类型为C程序员提供了常见的Objective-C类,如NSString、NSArray和NSBundle。C程序员使用指针和助手函数来创建、操作和销毁这些Objective-C类的实例。苹果公司称之为“免费桥接”。它们遵循一个常见的命名约定:“CF”前缀+类名+“Ref”后缀,其中“CF”代表“Core Foundation”,“Ref”是“Reference”的缩写,这意味着它是一个指针。
#2
13
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/文档/ CoreFoundation /概念/必要CFDesignConcepts /文章/ 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.
例如,CFString是一种不透明类型,因为它封装字符数组、保持其长度、编码等,但不允许用户直接访问这些值。相反,它提供了访问或操作内部字段并将相关信息返回给用户的方法。
#3
2
It's a future-declared structure. For example:
这是一个future-declared结构。例如:
typedef struct CFBundle *CFBundleRef;
Without the actual definition of "struct CFBundle", your code cannot access anything within a CFBundleRef pointer. This is opaque.
如果没有“struct CFBundle”的实际定义,代码就无法访问CFBundleRef指针中的任何内容。这是不透明的。
#1
68
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:
“不透明类型”是指结构体或类没有完整定义的类型。在C、c++和Objective-C中,您可以通过使用forward声明告诉编译器稍后将定义一个类型:
// 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
:
编译器除了声明指向它的指针之外,没有足够的信息可以让您直接对结构体或类执行任何操作,但这通常是您需要做的全部工作。这允许库和框架创建者隐藏实现细节。库或框架的用户然后调用helper函数来创建、操作和销毁前声明的结构体或类的实例。例如,框架创建者可以为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.
作为核心基础框架的一部分,Apple通过不透明的类型为C程序员提供了常见的Objective-C类,如NSString、NSArray和NSBundle。C程序员使用指针和助手函数来创建、操作和销毁这些Objective-C类的实例。苹果公司称之为“免费桥接”。它们遵循一个常见的命名约定:“CF”前缀+类名+“Ref”后缀,其中“CF”代表“Core Foundation”,“Ref”是“Reference”的缩写,这意味着它是一个指针。
#2
13
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/文档/ CoreFoundation /概念/必要CFDesignConcepts /文章/ 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.
例如,CFString是一种不透明类型,因为它封装字符数组、保持其长度、编码等,但不允许用户直接访问这些值。相反,它提供了访问或操作内部字段并将相关信息返回给用户的方法。
#3
2
It's a future-declared structure. For example:
这是一个future-declared结构。例如:
typedef struct CFBundle *CFBundleRef;
Without the actual definition of "struct CFBundle", your code cannot access anything within a CFBundleRef pointer. This is opaque.
如果没有“struct CFBundle”的实际定义,代码就无法访问CFBundleRef指针中的任何内容。这是不透明的。