信息隐藏 [英] Information hiding

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

问题描述

大家好,我想知道如何在C中实现一个简单的模式:

信息隐藏,隐藏数据结构的细节实现。

怎么能我这样做?我还读过,最好只使用struct

并避免使用typedef结构。有人可以帮忙吗?使用例如:

struct list {

int my_data;

struct list * next;

}

谢谢,

Mattia

Hi everybody, I''m wondering how to realize a simple pattern in C:
information hiding, to hide detail implementations of a data structure.
How can I do that? I''ve also read that is preferred to use only struct
and avoid typedef struct. Can anyone help? Use as example:
struct list {
int my_data;
struct list *next;
}
Thanks,
Mattia

推荐答案

4月8日上午8:35,mattia< ger ... @ gmail.comwrote:
On Apr 8, 8:35 am, mattia <ger...@gmail.comwrote:

大家好,我想知道如何在C中实现一个简单的模式:

信息隐藏,隐藏数据结构的详细实现。

我该怎么做?我还读过,最好只使用struct

并避免使用typedef结构。有人可以帮忙吗?使用例如:

struct list {

int my_data;

struct list * next;}


谢谢,

Mattia
Hi everybody, I''m wondering how to realize a simple pattern in C:
information hiding, to hide detail implementations of a data structure.
How can I do that? I''ve also read that is preferred to use only struct
and avoid typedef struct. Can anyone help? Use as example:
struct list {
int my_data;
struct list *next;}

Thanks,
Mattia



在你的头文件中,你包含了一个不完整的ADT定义

并声明将创建和修改

类型对象的函数:


/ ** list.h * /


#ifndef LIST_H

#define LIST_H


struct list;

typedef struct list list;


extern list * newList(void);

extern void deleteList(list ** theList);


extern void addValue( list * theList,int value);

extern int getValue(list * theList);

extern void removeValue(list * theList,int value);


extern list * getNext(list * theList);


/ *如果列表中没有元素则返回TRUE * /

extern int listEmpty(list * theList);


/ *如果在列表末尾则返回TRUE * /

exte rn int listEnd(list * theList);


#endif


然后你的实现文件将完成列表的定义:


/ ** list.c * /


#include< stdlib.h>

#include" list。 h"


结构列表

{

int my_data;

struct list * next; < br $>
}


list * newList(void)

{

list * p = malloc(sizeof * p);

if(p)

{

p-> my_data = 0;

p - > next = NULL;

}


返回p;

}


等等等。


您应该能够从这里找出其余部分。

In your header file, you include an incomplete definition of an ADT
and declare the functions that will create and modify objects of that
type:

/** list.h */

#ifndef LIST_H
#define LIST_H

struct list;
typedef struct list list;

extern list *newList(void);
extern void deleteList(list **theList);

extern void addValue(list *theList, int value);
extern int getValue(list *theList);
extern void removeValue(list *theList, int value);

extern list *getNext(list *theList);

/* return TRUE if no elements in list */
extern int listEmpty(list *theList);

/* return TRUE if at end of list */
extern int listEnd(list *theList);

#endif

Then your implementation file will complete the definition of list:

/** list.c */

#include <stdlib.h>
#include "list.h"

struct list
{
int my_data;
struct list *next;
}

list *newList(void)
{
list *p = malloc(sizeof *p);
if (p)
{
p->my_data = 0;
p->next = NULL;
}

return p;
}

etc., etc., etc.

You should be able to figure out the rest from here.




2008年4月8日13:35:26 GMT,mattia< ge **** @ gmail.comwrote:

On 08 Apr 2008 13:35:26 GMT, mattia <ge****@gmail.comwrote:

> Hi大家好,我想知道如何在C中实现一个简单的模式:信息隐藏,隐藏细节实现o数据结构。
我该怎么做?我还读过,最好只使用struct
并避免使用typedef结构。有人可以帮忙吗?使用例如:
struct list {

int my_data;

struct list * next;
}
>Hi everybody, I''m wondering how to realize a simple pattern in C:
information hiding, to hide detail implementations of a data structure.
How can I do that? I''ve also read that is preferred to use only struct
and avoid typedef struct. Can anyone help? Use as example:
struct list {
int my_data;
struct list *next;
}



信息隐藏是指某人有权访问您的结构,但是没有访问
结构中包含的某些数据但没有访问权限通过你的界面。


你可以通过编写实现

链表的功能来实现这一目的。喜欢:

list * Initialize(int my_data);

void AddItem(list * l,int my_data);

void PrintList(list l );


在C中执行此操作的难点在于,当您向某人提供对结构实例的引用时,该人现在可以访问

结构中的所有内容,并且无法保证此人

将使用您的功能。


一种解决方法是使用静态全局变量。一个静态的

global是定义它的文件的本地,这意味着它是其他人隐藏的
。当然,这将使用全局变量时遇到的所有

典型限制。


实现信息隐藏的另一种(更好的)方法是通过

误导。编写所有链接列表函数,但不是

使用函数引用链接的

列表的实例,让它们采用标识符(如int) 。然后你

维护地图,将标识符转换为正确的

参考。

Information hiding is the idea that someone has access to your struct,
but does not have access to some of the data contained inside the
struct without going through your interface.

You can accomplish this by writing the functions which implement the
linked list. Like:
list* Initialize(int my_data);
void AddItem(list *l, int my_data);
void PrintList(list l);

The difficulty of doing this in C is that when you give someone a
reference to an instance of your struct, that person now has access to
everything in the struct and there is no guarantee that the person
will use your functions.

One way around this is to use static global variables. A static
global is local to the file which defines it, which means that it is
hidden from everyone else. Of course this will have all of the
typical restrictions you encounter when using global variables.

Another (better) way to implement information hiding is through
misdirection. Write all of your linked list functions but instead of
having the functions take a reference to an instance of the linked
list, have them take an identifier (like an int). And then you
maintain the map which converts the identifier to the proper
reference.


好的,谢谢建议,一切都清楚。出于同样的原因,在我的标题

文件中,我声明extern我让用户调用的函数。然后在我的

实现文件中,我将不得不实现各种函数,

没有extern特殊字符,_but_我的静态字符

私人职能,不是吗?
Ok, thanks for the advice, all clear. For the same reason in my header
file I declare extern the functions that I let the user call. Then in my
implementation file I will have to implement the various functions,
without the extern special character, _but_ the static character for my
private functions, isn''t it?


这篇关于信息隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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