C++ 类函数地址打印

shihyuyao shihyuyao 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include <iostream>

using std::cout;
using std::endl;

class TestClass {
void MyFunc(void);

public:
void PrintMyFuncAddress(void);
};

void TestClass::MyFunc(void) {
return;
}

void TestClass::PrintMyFuncAddress(void) {

void (TestClass::*ptrtofn)() = &TestClass::MyFunc;
cout << ptrtofn << endl;
cout << &ptrtofn << endl;
cout << (void *&)ptrtofn << endl;
printf("%p\n", &TestClass::MyFunc);
}

int main(void) {
TestClass a;

a.PrintMyFuncAddress();
return 0;
}



打印结果:
1
0x7fff93642c60
0x4009ba // (void *&)ptrtofn
0x4009ba

有几个疑问
1. 为什么 cout << ptrtofn << endl; 是1 , 在C语言中 ptrtofn 不是代表函数指针指向函数地址但在c++ 为什么是打印出1
2. 0x7fff93642c60 // 这地址是代表什么?
3. 0x4009ba // (void *&)ptrtofn , 为什么要 (void *&)这样转类型?
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
shihyuyao
C++ 语言 65,214 成员 250,514 内容