当前位置:网站首页>Master the technology of Dachang in one class Commission and reflection
Master the technology of Dachang in one class Commission and reflection
2022-08-05 18:45:00 【CPP programming】

UseC language, understand the delegation mechanism from the bottom layer
// Commission 1: Computer Science and Technology Education void computerEdu(const char* name) { printf("%s study: operating system\n", name); printf("%s study: computer compositionPrinciple\n", name); printf("%s study: data structure\n", name); printf("%s study: data logic circuit\n", name); printf("%s study: compilation principle\n", name); }// Delegate 2: Physical Education void tiYuEdu(const char* name) { printf("%s study: basketball\n"); printf("%s study: athletics\n");printf("%s study: Baduanjin\n"); printf("%s study: football\n");}void xiangQing(const char* name) { printf("%s, 10,000 characters are omitted here....\n", name); }// university("Zhang San", computerEdu); // Execute:stuNmae = "Zhang San" edu = computerEdu void university(const char* stuNmae, void(*edu)(const char*)) { edu(stuNmae); }int main(void) { university("Zhang San", computerEdu); university("Zhang San", tiYuEdu); university("Zhang San", xiangQing); }
UseC language, understand the reflection mechanism from the bottom layer
struct node { const char* name; void (*func)(); };struct node nodes[] = { {"fishing", diaoYu}, {"code", luMa}, {"eat", eat},{"Swimming", youyong}, };// Parameters: favorite activity name void findLove(const char* name) { int count = sizeof(nodes) / sizeof(nodes[0]); for (int i = 0;i < count; i++) { if (strcmp(name, nodes[i].name) == 0) { nodes[i].func(); return; } } }Optimization:Use the compiler's precompiled instructions to define the data segment (compiled only by gcc/g++support)
#include #include #include typedef void(*calc_t)(int, int); typedef struct { const char* name; calc_t fn;} node_t; // only supported by gcc!#define SEC __attribute__((__section__("ss"), aligned(sizeof(node_t)))) void f1(int a, int b) { printf("%s %d %d %d\n", __func__, __LINE__, a, b); }void f2(int a, int b) { printf("%s %d %d %d\n", __func__, __LINE__, a, b); }// The compiler will automatically provide __start_ss, __stop_ss mark the start and end addresses of segment ss extern size_t __start_ss; extern size_t __stop_ss;// The structure variable a is located in the custom section ss node_t a SEC = { "cmd1", f1 }; void do_callback(const char* name) { size_t i; for (node_t* p = &__start_ss; p < &__stop_ss; p++) {if (!strcmp(p->name, name)) { return p->fn(1, 2); } } }// The structure variable b is located in the custom segment ss node_t b SEC = { "cmd2", f2 }; int main(int argc, char** argv) { do_callback("cmd2"); return 0; } Today's sharing is here, everyone should learn C language/C++~
Welcome partners who change careers and learn programming, learn and grow faster with more information than pondering on your own!
For those who are ready to learn C/C++ programming, if you want to better improve your core programming skills (internal skills), you might as well start now!
C Language From Beginner to Proficient (C Language Introduction C Language Tutorial C Language Zero Basic C Language Basic C Language Learning C
Organize and Share(Source code of many years of study, project actual combat video, project notes, basic introductory tutorial) Add Junyang to get it~
C language C++ programming learning exchange circle, Penguin Junyang: 763855696【Click to enter】
边栏推荐
猜你喜欢
随机推荐
数据库的基础学习1:select语句的查询
从中序与后序遍历序列构造二叉树
分析LED透明屏VS常规显示屏优劣
Technology Sharing | How to use Json for data interaction in interface testing?
力扣每日一题-第49天-724. 寻找数组的中心下标
手撕神经网络 numpy
面试结束前被问「你有哪些要问我的?」该怎么办?这样回答你就凉了
【CC3200AI 实验教程3】疯壳·AI语音人脸识别(会议记录仪/人脸打卡机)-CC3200简介
[ACTF2020 新生赛]Upload 1
什么是事务?
包载信使mRNA的多西环素纳米脂质体|雷公藤红素纳米脂质体RNA核糖核酸(实验原理)
有主键索引cpu 还是100%
阿里巴巴十亿级并发系统设计手册已开源(2022最新版)
ArcGIS Pro scripting tool (11)) of the modified layer value symbol annotations
Firewall destination address translation and source address translation
(22年纯享)阿里巴巴十亿级并发系统设计手册已开源
From the sequence with the sequence structure binary tree traversal sequence
云渲染平台是互联网和云计算的发展产物
(c) meet structure
uniapp中用canvas实现小球碰撞的小动画









