成员函数指针之疑惑

匚匚 2011-09-07 11:22:07

#include <iostream>

using namespace std;

class test
{
public:
void print(){}

};
typedef void(test::*ptr)();

int main()
{
int a=8;
ptr p=&test::print;
cout<<p<<endl; //1
cout<<(&test::print)<<endl; //1
cout<<sizeof(p)<<endl;//8
cout<<sizeof(&a)<<endl;//4
cout<<&a;//0x....
return 0;

}
...全文
126 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
十八道胡同 2011-09-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lcl_data 的回复:]
用printf, cout当做bool处理了

为什么count会把它当bool呢?
[/Quote]
这里哪里有解释?
十八道胡同 2011-09-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 pathuang68 的回复:]
参考:
类的普通成员函数的指针
[/Quote]
写的不错。
pathuang68 2011-09-07
  • 打赏
  • 举报
回复
十八道胡同 2011-09-07
  • 打赏
  • 举报
回复
用printf, cout当做bool处理了

为什么count会把它当bool呢?
ken_scott 2011-09-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 healer_kx 的回复:]
成员函数指针 不是 指针。。。
[/Quote]
所以 不能完全当指针要理解
看看这个 虽然这里没有你要的东西
http://blog.csdn.net/eroswang/article/details/4153356

坐等别人来解答
we_sky2008 2011-09-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zhw952 的回复:]
输出的为什么是8?这个指针为什么不是指针,是否与实现有关,比如加入了偏移(结构体)
[/Quote]
是的,和实现相关
VC6下是4
DEV下是8
匚匚 2011-09-07
  • 打赏
  • 举报
回复
输出的为什么是8?这个指针为什么不是指针,是否与实现有关,比如加入了偏移(结构体)
匚匚 2011-09-07
  • 打赏
  • 举报
回复
若输出格式是%d呢
[Quote=引用 2 楼 we_sky2008 的回复:]
C/C++ code

#include <iostream>

using namespace std;

class test
{
public:
void print(){}

};
typedef void(test::*ptr)();

int main()
{
int a=8;
ptr p=&test::print;
……
[/Quote]
we_sky2008 2011-09-07
  • 打赏
  • 举报
回复

#include <iostream>

using namespace std;

class test
{
public:
void print(){}

};
typedef void(test::*ptr)();

int main()
{
int a=8;
ptr p=&test::print;
printf("%p\n", p);//用printf, cout当做bool处理了
printf("%p\n", &test::print);//用printf,cout当做bool处理了
cout<<sizeof(p)<<endl;//8
cout<<sizeof(&a)<<endl;//4
cout<<&a;//0x....

system("pause");
return 0;
}


healer_kx 2011-09-07
  • 打赏
  • 举报
回复
成员函数指针 不是 指针。。。
we_sky2008 2011-09-07
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 lcl_data 的回复:]
引用 7 楼 lcl_data 的回复:
用printf, cout当做bool处理了

为什么count会把它当bool呢?

这里哪里有解释?
[/Quote]
以前在网上看到过,也可以简单验证下,
比如type (CLS::*pmf)(para_list) = NULL;
这时用cout<<pmf;就会输出0,而非零时都是输出1,

因为成员函数指针和实现相关,所以上面的说法并不具有说服力
另外,在《深度探索对象模型》中,侯杰也指出在输出类成员指针时应该使用printf
Enter空格 2011-09-07
  • 打赏
  • 举报
回复
真失败,不能编辑,又少贴代码了。
Enter空格 2011-09-07
  • 打赏
  • 举报
回复
楼主,通过反汇编,我只能告诉你 VC6对这段代码都做了什么。
但目前,没法告诉你,它为什么这么做。
0040180C call @ILT+40(std::basic_ostream<char,std::char_traits<char> >::operator<<) (0040102d)
00401811 mov ecx,eax
00401813 call @ILT+490(std::basic_ostream<char,std::char_traits<char> >::operator<<) (004011ef)
24: cout<<sizeof(p)<<endl;//8
00401818 push offset @ILT+210(std::endl) (004010d7)
0040181D push 4
0040181F mov ecx,offset std::cout (00477a10)
00401824 call @ILT+265(std::basic_ostream<char,std::char_traits<char> >::operator<<) (0040110e)
00401829 mov ecx,eax
0040182B call @ILT+490(std::basic_ostream<char,std::char_traits<char> >::operator<<) (004011ef)
25: cout<<sizeof(&a)<<endl;//4
00401830 push offset @ILT+210(std::endl) (004010d7)
00401835 push 4
00401837 mov ecx,offset std::cout (00477a10)
0040183C call @ILT+265(std::basic_ostream<char,std::char_traits<char> >::operator<<) (0040110e)
00401841 mov ecx,eax
00401843 call @ILT+490(std::basic_ostream<char,std::char_traits<char> >::operator<<) (004011ef)
26: cout<<&a;//0x....
00401848 lea edx,[ebp-4]
0040184B push edx
0040184C mov ecx,offset std::cout (00477a10)
00401851 call @ILT+65(std::basic_ostream<char,std::char_traits<char> >::operator<<) (00401046)
27: return 0;

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧