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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c 语言程序设计阚道宏,C语言用宏实现静态多态

發布時間:2025/4/5 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c 语言程序设计阚道宏,C语言用宏实现静态多态 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

綜述

多態性是面向對象程序設計的一個重要特征。

在C++語言中,多態指的是:具有不同功能的函數可以用同一個函數名,可以用一個函數名調用不同內容的函數。

C++的多態分為兩種:

1. 靜態多態性(編譯時多態):在程序編譯時系統就能決定調用的是哪個函數,因此稱為編譯時多態性

2. 動態多態性:在程序運行過程中才動態確定操作指針指向的對象,又被稱為運行時多態

C++實現多態可以用虛函數,抽象類,覆蓋以及模板實現

C語言是面向過程的語言,但同樣可以實現多態性,可以通過宏實現編譯時多態,用函數指針實現動態多態

C語言用宏實現編譯時多態

例如以下例子是用宏實現雙向鏈表的一部分

#include

#include

#define INIT_LIST_TYPE(type) ? ? ? ? ? ? ? ? ? ?\

typedef struct list_element_##type { ? ? ? ?\

struct list_element_##type* next, *pre; \

void* val; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?\

} list_element_##type; ? ? ? ? ? ? ? ? ? ? ?\

typedef struct list_head_##type { ? ? ? ? ? \

list_element_##type* elem, *last; ? ? ? \

int length; ? ? ? ? ? ? ? ? ? ? ? ? ? ? \

} list_head_##type;

#define list_element(type) \

list_element_##type

#define list_head(type) \

list_head_##type

#define LIST_FUNC_DECLARE(type) ? \

list_head_##type* init_list_##type();

#define LIST_FUNC_INIT(type) ? \

_init_list(type)

#define init_list(type) init_list_##type()

#define _init_list(type) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?\

list_head_##type* init_list_##type() ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?\

{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? \

list_head_##type* h = (list_head_##type*)calloc(1, sizeof(list_head_##type)); \

h->last = h->elem = NULL; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? \

h->length = 0; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?\

return h; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? \

}

#endif

調用

INIT_LIST_TYPE(int);

LIST_FUNC_INIT(int);

INIT_LIST_TYPE(float);

LIST_FUNC_INIT(float);

#define context_bam_elem list_element(int)

#define list_head_t list_head(int)

#define list_element_f_t list_element(float)

#define list_head_f_t list_head(float)

int main()

{

list_head_t* h = init_list(int);

list_head_f_t* h1 = init_list(float);

return 0;

}

---------------------

作者:晏九

來源:CSDN

原文:https://blog.csdn.net/weixin_42670653/article/details/81385745

版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

總結

以上是生活随笔為你收集整理的c 语言程序设计阚道宏,C语言用宏实现静态多态的全部內容,希望文章能夠幫你解決所遇到的問題。

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