6.3w+
社区成员
显然,编译器找不到定义时,会试图进行隐式转换,
而成员函数指针能转化为bool类型,而bool类型能打印:
class A
{
public:
void fun()
{
cout<<"A::fun()\n";
}
};
/*
std::ostream& operator<<(std::ostream& os,void(A::* const& mpf)())
{
char str[32];
sprintf(str,"%p",mpf);
return os<<str;
}*/
int main()
{
printf("%p\n",&A::fun);
cout<<std::boolalpha<<&A::fun<<endl;
return 0;
}
class A
{
public:
void fun()
{
cout<<"A::fun()\n";
}
};
std::ostream& operator<<(std::ostream& os,void(A::* const& mpf)())
{
char str[32];
sprintf(str,"%p",mpf);
return os<<str;
}
int main()
{
printf("%p\n",&A::fun);
cout<<&A::fun<<endl;
return 0;
}
#define Print_P(p) printf("%p\n", p)
int main(int argc, _TCHAR* argv[])
{
Print_P(&Base::base_fun);
Print_P(&Derived::base_fun);
Print_P(&Derived::derived_fun);
Print_P(&Base::vf);
Print_P(&Base::vf1);
Print_P(&Derived::vf);
Print_P(&Derived::vf1);
return 0;
}