c 函数别名
来源:互联网 发布:网络诗歌在哪里发布 编辑:程序博客网 时间:2024/05/21 19:48
int test(int num) {
return num;
}
1.define
#define TEST test
TEST(33);
2.函数地址指针
int (*testv2)(int) = test;
testv2(33);
3.__attribute__ ((__weakref__(""))), 弱引用
static __typeof(test) testv3 __attribute__ ((_weakref_("test")))
if (testv3) {
testv3(33);
}
其中__typeof会返回变量或者函数的实际类型信息,如
int num;
__typeof(num) num1 = 1; // int
int test(int num) {
return 0;
}
__typeof(test) test1; // int (*test1)(int)
完整测试代码:
#include <stdio.h>int test(int num) { return num;}#define TEST testint (*testv2)(int) = test;static __typeof(test) testv3 __attribute__((__weakref__("test")));int main(int argc, char **argv) { // version 1 printf("version 1 value %d\n", TEST(33)); // version 2 printf("version 2 value %d\n", testv2(33)); // version 3 if (testv3) printf("version 3 value %d\n", testv3(33)); return 0;}
注:
如果上诉测试代码version 3发现没有执行,那么请确认下自己是否是用g++进行编译的
如果是用g++进行编译,那么会出现test函数的名字是c++命名规范,而__weakref__("test")是以c命名规范,
因此要想在g++编译器下同样生效,可以在test函数外围包一层extern "C" {}即可
0 0
- c 函数别名
- C语言中给函数取别名
- C语言函数指针 与typedef别名
- 创建函数别名
- C#using别名指令
- C/C++类型别名
- 细嚼慢咽C++primer(4)——类(1):构造函数,类型别名
- C语言大千世界————01开篇之函数别名
- C语言结构体及typedef关键字定义结构体别名和函数指针的应用
- 高效C编程(上) 基础数据类型,Loop,寄存器分配,函数调用,指针别名 ARM
- C语言——函数指针&联合体&别名&结构体&结构体指针
- C/C++中如何使用typedef给函数指针取别名使其更具可读性
- GCC如何支持函数别名
- shell之别名和函数
- c语言之typedef别名
- c中的参数别名问题
- C语言大世界—————09指针类型之函数指针+函数别名 福利篇
- 几个有用的Shell别名和函数
- 快上车!EventBus使用
- MySQL查询根据名字段重复内容只显示最新的一条
- 1068. Find More Coins (30)
- Tanks Unity Tutorial
- e.target
- c 函数别名
- npm的使用
- js原型与原型链的理解
- 不同操作系统换行符的区别
- js图片压缩-H5
- 补3
- Unity自定义宏
- 【CRM】02- WEB UI 界面布局简介
- git无法切换分支