不透明的指针 [英] Opaque pointers

查看:109
本文介绍了不透明的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释一下这是一个不透明的指针以及它是如何在

C中实现的吗?


我在这里读到了这个概念预订C接口和

实施。


这是本书中的一个例子(list.h - 可从网站获得):


#define T List_T


typedef struct T * T;

struct T {

T rest;

void * first;

};


extern T List_append(T list,T tail);

extern T List_copy(T列表);

extern T List_list(void * x,...);

..

..

等...


似乎List_T背后的结构现在已隐藏在应用程序中

使用这些函数。无法访问List_T的内容(或

dereferenced?)。


但我不知道为什么不这样做。如果我有权访问头文件,那么我是不是可以取消引用结构的内容?或者我错过了

不透明的含义?


非常感谢任何答案。

Can anyone explain what''s an opaque pointer and how it''s implemented in
C?

I read about this concept in the book "C interfaces and
implementations".

Here''s an example from the book (list.h - available from the website):

#define T List_T

typedef struct T *T;
struct T {
T rest;
void *first;
};

extern T List_append (T list, T tail);
extern T List_copy (T list);
extern T List_list (void *x, ...);
..
..
and so on...

It seems the struct behind List_T is now hidden from the application
using these functions.The contents of List_T cannot be accessed (or
dereferenced?).

But I don''t see why not. If I have access to the header file, wouldn''t
I be able to dereference the contents of the structure? Or am I missing
the meaning of opaque?

Any answers is much appreciated.

推荐答案

chankl写道:
chankl wrote:

任何人都可以解释什么是不透明指针以及它是如何在

C?


我在C接口和

实施一书中读到了这个概念。

这里有一本书中的例子(list.h - 可从网站获得):


#define T List_T


typedef struct T * T;

struct T {

T rest;

void * first;

};


extern T List_append(T list,T tail);

extern T List_copy(T list);

extern T List_list(void * x,...);





等......


看来List_T背后的结构现在隐藏在应用程序中

使用thes电子功能。无法访问List_T的内容(或

取消引用?)。


但我不明白为什么不这样做。如果我有权访问头文件,那么我是不是可以取消引用结构的内容?或者我错过了

不透明的含义?


我们非常感谢任何答案。
Can anyone explain what''s an opaque pointer and how it''s implemented in
C?

I read about this concept in the book "C interfaces and
implementations".

Here''s an example from the book (list.h - available from the website):

#define T List_T

typedef struct T *T;
struct T {
T rest;
void *first;
};

extern T List_append (T list, T tail);
extern T List_copy (T list);
extern T List_list (void *x, ...);
.
.
and so on...

It seems the struct behind List_T is now hidden from the application
using these functions.The contents of List_T cannot be accessed (or
dereferenced?).

But I don''t see why not. If I have access to the header file, wouldn''t
I be able to dereference the contents of the structure? Or am I missing
the meaning of opaque?

Any answers is much appreciated.



在你的标题文件中,你会有以下内容:


foo.h

-----

typedef struct foo foo;


foo * create_foo(void);

void destroy_foo(foo * ptr);

void use_foo(foo * ptr);


''struct foo''的实际定义在源文件中:


foo.c

-----

struct foo

{

/ * .. 。* /

};


所以它只是foo.c才知道结构foo的内容是什么

是。内容是隐藏的,即不透明的,仅仅是因为
包括foo.h - 所有你能做的就是传递指针。


-

rh

In your header file, you''d have something along the lines of:

foo.h
-----
typedef struct foo foo;

foo *create_foo(void);
void destroy_foo(foo *ptr);

void use_foo(foo *ptr);

The actual definition of ''struct foo'' goes in the source file:

foo.c
-----
struct foo
{
/* ... */
};

So that it''s only foo.c that knows what the contents of a struct foo
are. The contents are hidden, ie opaque, from everything that merely
includes foo.h - all you can do is pass pointers around.

--
rh


Richard Harnden写道:
Richard Harnden wrote:

>

chankl写道:
>
chankl wrote:

任何人都可以解释什么是'不透明指针以及它是如何在
$ b $中实现的b C?


我在C接口和

实施一书中读到了这个概念。


这是本书中的一个例子(list.h - 可从网站获得):


#define T List_T


typedef struct T * T;

struct T {

T rest;

void * first;

};


extern T List_append(T list,T tail);

extern T List_copy(T list);

extern T List_list( void * x,.. 。);





等......


似乎是现在,使用这些函数,List_T后面的struct隐藏在应用程序

中。无法访问List_T的内容(或

dereferenced?)。


但我不明白为什么不。如果我有权访问头文件,那么我是不是可以取消引用结构的内容?或者我错过了

不透明的含义?


任何答案都非常感谢。
Can anyone explain what''s an opaque pointer and how it''s implemented in
C?

I read about this concept in the book "C interfaces and
implementations".

Here''s an example from the book (list.h - available from the website):

#define T List_T

typedef struct T *T;
struct T {
T rest;
void *first;
};

extern T List_append (T list, T tail);
extern T List_copy (T list);
extern T List_list (void *x, ...);
.
.
and so on...

It seems the struct behind List_T is now hidden from the application
using these functions.The contents of List_T cannot be accessed (or
dereferenced?).

But I don''t see why not. If I have access to the header file, wouldn''t
I be able to dereference the contents of the structure? Or am I missing
the meaning of opaque?

Any answers is much appreciated.



在你的标题文件中,你会有以下内容:


foo.h
-----

typedef struct foo foo;


foo * create_foo(void);

void destroy_foo(foo * ptr);


void use_foo(foo * ptr);


''struct foo'的实际定义进入源文件:


foo.c

-----

struct foo

{

/ * ... * /

};


所以它只是foo.c知道结构foo

的内容是什么。内容是隐藏的,即不透明的,仅仅是因为
包括foo.h - 所有你能做的就是传递指针。


In your header file, you''d have something along the lines of:

foo.h
-----
typedef struct foo foo;

foo *create_foo(void);
void destroy_foo(foo *ptr);

void use_foo(foo *ptr);

The actual definition of ''struct foo'' goes in the source file:

foo.c
-----
struct foo
{
/* ... */
};

So that it''s only foo.c that knows what the contents of a struct foo
are. The contents are hidden, ie opaque, from everything that merely
includes foo.h - all you can do is pass pointers around.



这是错的。

stuct类型的定义需要在头文件中。

否则,

void destroy_foo(foo * ptr);

并不意味着头文件中的任何内容。


-

pete

That''s wrong.
The defintion of the stuct type needs to be in the header file.
Otherwise,
void destroy_foo(foo *ptr);
wouldn''t mean anything in the header file.

--
pete


pete写道:
pete wrote:

>

Richard Harnden写道:
>
Richard Harnden wrote:


chankl写道:

chankl wrote:

Can任何人解释什么是不透明的指针以及它是如何在

C中实现的?

>

我读到了这个概念在书中C接口和

实现。

>

这是本书中的一个例子(list.h - 可从网站获得):

>

#define T List_T

>

typedef struct T * T;

struct T {

T re st;

void * first;

};

>

extern T List_append(T list,T尾巴);

extern T List_copy(T列表);

extern T List_list(void * x,...);





等......

>

看来List_T背后的结构现在隐藏了应用程序

使用这些函数。无法访问List_T的内容(或

dereferenced?)。

>

但我不明白为什么不。如果我有权访问头文件,那么我是不是可以取消引用结构的内容?或者我错过了

不透明的含义?

>

我们非常感谢任何答案。

> ;
Can anyone explain what''s an opaque pointer and how it''s implemented in
C?
>
I read about this concept in the book "C interfaces and
implementations".
>
Here''s an example from the book (list.h - available from the website):
>
#define T List_T
>
typedef struct T *T;
struct T {
T rest;
void *first;
};
>
extern T List_append (T list, T tail);
extern T List_copy (T list);
extern T List_list (void *x, ...);
.
.
and so on...
>
It seems the struct behind List_T is now hidden from the application
using these functions.The contents of List_T cannot be accessed (or
dereferenced?).
>
But I don''t see why not. If I have access to the header file, wouldn''t
I be able to dereference the contents of the structure? Or am I missing
the meaning of opaque?
>
Any answers is much appreciated.
>



在你的标题文件中,你会有以下内容:


foo.h
-----

typedef struct foo foo;


foo * create_foo(void);

void destroy_foo(foo * ptr);


void use_foo(foo * ptr);


''struct foo'的实际定义进入源文件:


foo.c

-----

struct foo

{

/ * ... * /

};


所以它只是foo.c知道结构foo

的内容是什么。内容是隐藏的,即不透明的,仅仅是因为
包括foo.h - 所有你能做的就是传递指针。

In your header file, you''d have something along the lines of:

foo.h
-----
typedef struct foo foo;

foo *create_foo(void);
void destroy_foo(foo *ptr);

void use_foo(foo *ptr);

The actual definition of ''struct foo'' goes in the source file:

foo.c
-----
struct foo
{
/* ... */
};

So that it''s only foo.c that knows what the contents of a struct foo
are. The contents are hidden, ie opaque, from everything that merely
includes foo.h - all you can do is pass pointers around.



那是错的。

stuct类型的定义需要在头文件中。

否则,

void destroy_foo(foo * ptr);

并不代表​​头文件中的任何内容。


That''s wrong.
The defintion of the stuct type needs to be in the header file.
Otherwise,
void destroy_foo(foo *ptr);
wouldn''t mean anything in the header file.



一般来说,它只是对象定义和函数定义

不属于头文件。

Typedef和枚举是两种定义

在头文件中通常都可以。


我不太熟悉内联功能。

我认为他们也可以输入头文件,

但是我不确定。


-

pete

In general, it''s only object defintions and function definitions
that don''t belong in header files.
Typedefs and enums are two kinds of definitions
that are typically OK in header files.

I''m not too familiar with inline functions.
I think they can go in header files too,
but I''m not sure.

--
pete


这篇关于不透明的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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