C中的不透明指针是什么?

时间:2022-11-07 17:02:29

Possible Duplicate:
What is an opaque value?

可能重复:什么是不透明值?

May I know the usage and logic behind the opaque pointer concept in C?

我可以知道不透明指针概念在C中的用法和逻辑吗?

1 个解决方案

#1


75  

An opaque pointer is one in which no details are revealed of the underlying data (from a dictionary definition: opaque: adjective; not able to be seen through; not transparent).

不透明指针是指不显示底层数据的指针(来自字典定义:不透明:形容词;不能被看穿;不透明的)。

For example, you may declare in a header file (this is from some of my actual code):

例如,您可以在头文件中声明(这来自我的一些实际代码):

typedef struct pmpi_s *pmpi;

which declares a type pmpi which is a pointer to the opaque structure struct pmpi_s, hence anything you declare as pmpi will be an opaque pointer.

它声明了一个类型pmpi,它是指向不透明结构结构pmpi_s的指针,因此您声明为pmpi的任何内容都是不透明的指针。

Users of that declaration can freely write code like:

该声明的用户可以*地编写如下代码:

pmpi xyzzy = NULL;

without knowing the actual "definition" of the structure.

不知道结构的实际“定义”。

Then, in the code that knows about the definition (ie, the code providing the functionality for pmpi handling, you can "define" the structure:

然后,在知道定义的代码中(例如,为pmpi处理提供功能的代码,您可以“定义”结构:

struct pmpi_s {
    uint16_t *data;     // a pointer to the actual data array of uint16_t.
    size_t sz;          // the allocated size of data.
    size_t used;        // number of segments of data in use.
    int sign;           // the sign of the number (-1, 0, 1).
};

and easily access the individual fields of it, something that users of the header file cannot do.

并且很容易地访问它的各个字段,这是头文件的用户不能做的。

More information can be found on the Wikipedia page for opaque pointers..

更多的信息可以在*上找到不透明指针。

The main use of it is to hide implementation details from users of your library. Encapsulation (despite what the C++ crowd will tell you) has been around for a long time :-)

它的主要用途是向库的用户隐藏实现细节。封装(尽管c++人群会告诉您)已经存在很长时间了:-)

You want to publish just enough details on your library for users to effectively make use of it, and no more. Publishing more gives users details that they may come to rely upon (such as the fact the size variable sz is at a specific location in the structure, which may lead them to bypass your controls and manipulate it directly.

您希望在库中发布足够的细节,以便用户有效地使用它,仅此而已。发布更多信息给用户提供了他们可能会依赖的细节(例如size变量sz位于结构中的特定位置,这可能导致他们绕过您的控件并直接操作它。

Then you'll find your customers complaining bitterly when you change the internals. Without that structure information, your API is limited only to what you provide and your freedom of action regarding the internals is maintained.

然后,当你更换内部设备时,你会发现你的客户在痛苦地抱怨。如果没有这些结构信息,您的API只局限于您提供的内容,并且您对内部内容的操作*将得到维护。

#1


75  

An opaque pointer is one in which no details are revealed of the underlying data (from a dictionary definition: opaque: adjective; not able to be seen through; not transparent).

不透明指针是指不显示底层数据的指针(来自字典定义:不透明:形容词;不能被看穿;不透明的)。

For example, you may declare in a header file (this is from some of my actual code):

例如,您可以在头文件中声明(这来自我的一些实际代码):

typedef struct pmpi_s *pmpi;

which declares a type pmpi which is a pointer to the opaque structure struct pmpi_s, hence anything you declare as pmpi will be an opaque pointer.

它声明了一个类型pmpi,它是指向不透明结构结构pmpi_s的指针,因此您声明为pmpi的任何内容都是不透明的指针。

Users of that declaration can freely write code like:

该声明的用户可以*地编写如下代码:

pmpi xyzzy = NULL;

without knowing the actual "definition" of the structure.

不知道结构的实际“定义”。

Then, in the code that knows about the definition (ie, the code providing the functionality for pmpi handling, you can "define" the structure:

然后,在知道定义的代码中(例如,为pmpi处理提供功能的代码,您可以“定义”结构:

struct pmpi_s {
    uint16_t *data;     // a pointer to the actual data array of uint16_t.
    size_t sz;          // the allocated size of data.
    size_t used;        // number of segments of data in use.
    int sign;           // the sign of the number (-1, 0, 1).
};

and easily access the individual fields of it, something that users of the header file cannot do.

并且很容易地访问它的各个字段,这是头文件的用户不能做的。

More information can be found on the Wikipedia page for opaque pointers..

更多的信息可以在*上找到不透明指针。

The main use of it is to hide implementation details from users of your library. Encapsulation (despite what the C++ crowd will tell you) has been around for a long time :-)

它的主要用途是向库的用户隐藏实现细节。封装(尽管c++人群会告诉您)已经存在很长时间了:-)

You want to publish just enough details on your library for users to effectively make use of it, and no more. Publishing more gives users details that they may come to rely upon (such as the fact the size variable sz is at a specific location in the structure, which may lead them to bypass your controls and manipulate it directly.

您希望在库中发布足够的细节,以便用户有效地使用它,仅此而已。发布更多信息给用户提供了他们可能会依赖的细节(例如size变量sz位于结构中的特定位置,这可能导致他们绕过您的控件并直接操作它。

Then you'll find your customers complaining bitterly when you change the internals. Without that structure information, your API is limited only to what you provide and your freedom of action regarding the internals is maintained.

然后,当你更换内部设备时,你会发现你的客户在痛苦地抱怨。如果没有这些结构信息,您的API只局限于您提供的内容,并且您对内部内容的操作*将得到维护。