不透明指针 Opaque Pointers
- 用来防止用户重复或不正确实现库提供的功能
- 所以属性皆为私有
- 现实常用
实现例子:
重点 ll_node_ 不对于,这样外面就不能直接操作,例如 sizeof malloc 等等
linkedlist.h
typedef struct ll_node_ ll_node_t;
void linkedlist_insertion(ll_node_t *current_node, ll_node_t *new_node);
ll_node_t * malloc_new_node();
linkedlist.c
#include <stdlib.h>
#include "linkedlist.h"
typedef struct ll_node_ {
int data;
struct ll_node_ *left;
struct ll_node_ *right;
} ll_node_t;
void linkedlist_insertion(ll_node_t *current_node, ll_node_t *new_node) {
return;
}
ll_node_t * malloc_new_node() {
return malloc(sizeof(ll_node_t));
}
如果外面直接用 例如 sizeof 就会编译错误
application.c
#include <stdio.h>
#include "linkedlist.h"
int main() {
ll_node_t *node1 = malloc_new_node();
ll_node_t *node2 = malloc_new_node();
linkedlist_insertion(node1, node2);
//printf("%d", sizeof(ll_node_t));
return 0;
}