【发布时间】:2016-12-23 21:10:03
【问题描述】:
在下面的代码中,
/**********linkedListImpl.c ***********/
#include"list/list.h"
#if defined(LINKED_LIST)
/***************** Representation - start ******************/
/* struct members are not visible to other .c files */
static struct DListNode{
void *item;
struct DListNode *next;
struct DListNode *prev;
};
/* Should be used in this .c file, only, so static */
static typedef struct DListNode DListNode;
static DListNode* createNode(void *);
static struct List{
DListNode *head;
int size; /*size attribute is not part of list definition, but quick way
to help user code */
}List;
.....
#endif
/************ list.h ************/
#ifndef LIST_H /* Header guard */
#define LIST_H
#include"type.h"
/***************** Usage-start ************/
#if defined(ARRAY) || (LINKED_LIST)
typedef struct List List;
#else
#error "Wrong list implementation macro name !!!"
#endif
...
#endif /* LIST_H */
List 类型在list.h 中声明,使用staticspecifier,
typedef struct List List;
并在linkedListImpl.c 中定义,使用staticspecifier,
static struct List{
DListNode *head;
int size;
}List;
目的:使List 符号仅通过指针(List*) 对用户(main.c) 可用。用户(main.c) 应该无法访问main.c 中的List 成员。
在linkedListImpl.c 中,符号DListNode 是使用static 说明符定义的,
static struct DListNode{
void *item;
struct DListNode *next;
struct DListNode *prev;
};
static typedef struct DListNode DListNode;
目的:对于符号DListNode,用户(main.c)既不能访问main.c中的DListNode成员,也不能通过DListNode*指针访问DListNode对象。
这是隐藏符号List 和DListNode 的正确方法吗?
注意:Here 是完整代码
【问题讨论】: