日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C面向对象之透明指针的运用

發布時間:2023/11/27 生活经验 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C面向对象之透明指针的运用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

不透明指針(opaque pointer)可以用來在C中實現封裝。

什么是不透明指針(opaque pointer)

從字面意思來看,“不透明”意味著看不到內部,因此“不透明指針”即看不到內部定義的指針。這樣說有些抽象,我們來看個例子:

#include <stdio.h>
typedef void   *opque_data;
typedef struct AAA *opque_stru;int main()
{opque_data data = 0;opque_stru stru = 0;printf("Hello, World! \n");return 0;
}

上面的程序可以build通過(也許你會覺得疑惑),data和stru 指針所指的type(AAA)細節是什么,我們不得而知。我們可以自由的操縱這種指針,但無法解引用,即無法查看指針所指向結構的內部信息,只有接口的實現才有這種特權。

透明指針面向對象應用

void *型指針作為一種通用的指針,可以和其它任何類型的指針(函數指針除外)相互轉化而不需要類型強制轉換,但不能對它進行解引用及下標操作。

在.h文件中聲明不包含任何實現細節的結構體,在.c中定義與數據結構的特定實現函數。用一個鏈表的代碼(來自《深入理解C指針》)來說明透明指針的用法。

下面鏈表的代碼中,link并沒有與具體的data綁定,而是關聯一個void *Data.

link.h

#ifndef LINK_H
#define LINK_Htypedef void* Data;  /* opaque pointer */typedef struct _linkedList LinkedList;
LinkedList* getLinkedListInstance();
void removeLinkedListInstance(LinkedList* list);
void addNode(LinkedList*, Data);
Data removeNode(LinkedList *);#endif

link.c

#include "link.h"
#include <stdlib.h>typedef struct _node{Data* data;   /*here use “Data data;” is also ok */struct _node* next;
} Node;struct _linkedList{Node* head;
};LinkedList* getLinkedListInstance(){LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));list->head = NULL;return list;
}void removeLinkedListInstance(LinkedList* list){Node *tmp = list->head;while(tmp != NULL){free(tmp->data); /* potential memeory leak, it denpeds on if any pointer inside real type of data */Node *current = tmp;tmp = tmp->next;free(current);}free(list);
}void addNode(LinkedList* list, Data data){Node *node = (Node *)malloc(sizeof(Node));node->data = data;if(list->head == NULL){list->head = node;node->next = NULL;}else{node->next = list->head;list->head = node;}  
}Data removeNode(LinkedList *list){if(list->head == NULL){return NULL;}else{Node* tmp = list->head;Data* data = tmp->data;list->head = list->head->next;free(tmp);   /* only free node, data is not free here */return data;  /* notes: data inside node returned, client need to free it,  */}
}

下面我們定義一個具體的node 數據類型Person.

person.h

#ifndef PERSON_H
#define PERSON_Htypedef struct _person{char* firstName;char* lastName;char* title;unsigned int age;
} Person;void initPerson(Person *personPtr, const char* firstNamePtr, const char* lastNamePtr, const char* titlePtr, unsigned int age);
void delePerson(Person *personPtr);
void dispPerson(Person *personPtr);#endif

person.c

#include "person.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>void initPerson(Person *personPtr, const char* firstNamePtr, const char* lastNamePtr, const char* titlePtr, unsigned int age)
{personPtr->firstName = (char *)malloc(strlen(firstNamePtr) + 1);strcpy(personPtr->firstName, firstNamePtr);personPtr->lastName = (char *)malloc(strlen(lastNamePtr) + 1);strcpy(personPtr->lastName, lastNamePtr);personPtr->title = (char *)malloc(strlen(titlePtr) + 1);strcpy(personPtr->title, titlePtr);personPtr->age = age;
}void delePerson(Person *personPtr)
{free(personPtr->firstName);free(personPtr->lastName);free(personPtr->title);
}void dispPerson(Person *personPtr)
{printf("%s info name: %s %s\t title: %s\tage: %d\n", personPtr->firstName, personPtr->firstName, personPtr->lastName, personPtr->title, personPtr->age);
}

main.c

#include "link.h"
#include "person.h"
#include <stdio.h>int main(int argc, char **argv)
{LinkedList *list = getLinkedListInstance();Person *person = (Person *)malloc(sizeof(Person));initPerson(person, "Ricky", "ZEK", "Acmen", 36);addNode(list, person);person = (Person *)malloc(sizeof(Person));initPerson(person, "John", "OPP", "Develop", 28);addNode(list, person);person = (Person *)malloc(sizeof(Person));initPerson(person, "Ami", "VIV", "pet", 2);addNode(list, person);person = removeNode(list);dispPerson(person);delePerson(person);free(person);person = removeNode(list);dispPerson(person);delePerson(person);free(person);removeLinkedListInstance(list); /*memory leak, you can think of it */return 0;
}

總結

以上是生活随笔為你收集整理的C面向对象之透明指针的运用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。