如何通过该类的对象使用指向成员函数的指针来打印成员函数的地址 [英] how to print the address of a member function using pointer to member function through a object of that class
本文介绍了如何通过该类的对象使用指向成员函数的指针来打印成员函数的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在屏幕上打印一个类成员函数的地址(
函数的某些对象的课程)。我的代码如下所示:
class MyClass
{
public :
void fun(空隙跨度>){};
};
void (MyClass :: * ptr)( void );
void main( void )
{
MyClass myObject;
ptr = MyClass :: fun;
cout<< myObject。* ptr<< ENDL;
// ...
}
编译时出错:
致命错误C1001:内部编译器错误(编译器文件'msc1.cpp',
第2701行)请在Visual C ++
帮助菜单上选择技术支持命令,或打开技术支持帮助文件以获取更多信息
我的问题是:
1.为什么我收到此错误?
2.我该怎么做(打印地址)?
解决方案
2)答案:
您需要声明一个名为fptr的指向成员的函数,如下所示:
void (MyClass :: * fptr)( void )=& MyClass :: fun;
打印地址只需拨打& fptr:
class MyClass
{
public :
void fun( void ){};
};
int main( void )
{
void (MyClass :: * fptr)( void )=& MyClass :: fun ;
cout<< & fptr<< ENDL;
return 0 ;
}
1)我建议阅读这篇非常好的教程: C ++教程:指向会员功能 [ ^ ]
Hi,
I'm trying to print on screen an address of class member function (the
function of certain object of course). My code looks like this:
class MyClass
{
public:
void fun(void){};
};
void (MyClass::*ptr)(void);
void main(void)
{
MyClass myObject;
ptr = MyClass::fun;
cout << myObject.*ptr << endl;
// ...
}
An error occures while compilation:
"fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp',
line 2701) Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information"
My questions are:
1. Why I got this error?
2. How should I do it (print address) properly?
解决方案
The answer to 2):
You need to declare a pointer-to-member function named "fptr" like this:
void (MyClass::*fptr) (void) = &MyClass::fun;
To print the address of it just call "&fptr":
class MyClass { public: void fun(void){}; }; int main(void) { void (MyClass::*fptr) (void) = &MyClass::fun; cout << &fptr << endl; return 0; }
To 1) I would recommend to read this very good tutorial: C++ Tutorial: Pointer-to-Member Function[^]
这篇关于如何通过该类的对象使用指向成员函数的指针来打印成员函数的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文