代码
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
//pointer_cast - 通过静态转换
template<typename dst_type,typename src_type>
dst_type pointer_cast(src_type src)
{
return *static_cast<dst_type*>(static_cast<void*>(&src));
}
//union_cast - 通过联合体的共享储存机制
template<typename dst_type,typename src_type>
dst_type union_cast(src_type src)
{
union{
src_type s;
dst_type d;
}u;
u.s = src;
return u.d;
}
void show()
{
cout<<"show()"<<endl;
}
class A
{
public:
static void work(){}
static void run(){}
void fn(){}
void fn1(){}
};
int main(void)
{
//全局函数地址
printf("show address : 0x%x\n", &show);
//类内静态函数地址
printf("A::run address : 0x%x\n", &A::run);
printf("A::work address : 0x%x\n", &A::work);
//类内普通函数地址(静态函数地址在普通函数后面)
void* p1 = pointer_cast<void*>(&A::fn);
void* p2 = union_cast<void*>(&A::fn1);
printf("A::fn address : 0x%x\n", p1);
printf("A::fn1 address : 0x%x\n", p2);
return 0;
}
子类继承父类的静态成员函数和普通成员函数,函数地址相同。
https://blog.csdn.net/chenqin158741019/article/details/8074945?reload
代码结果
![](https://i-blog.csdnimg.cn/blog_migrate/3229cb115ad98abc77c40c85f6fab6e8.png)