【发布时间】:2011-09-09 21:37:19
【问题描述】:
我是 c++ 新手。我想了解对象指针和指向成员函数的指针。我写了如下代码:
代码:
#include <iostream>
using namespace std;
class golu
{
int i;
public:
void man()
{
cout<<"\ntry to learn \n";
}
};
int main()
{
golu m, *n;
void golu:: *t =&golu::man(); //making pointer to member function
n=&m;//confused is it object pointer
n->*t();
}
但是当我编译它时,它显示了两个错误:
pcc.cpp: In function ‘int main()’:
pcc.cpp:15: error: cannot declare pointer to ‘void’ member
pcc.cpp:15: error: cannot call member function ‘void golu::man()’ without object
pcc.cpp:18: error: ‘t’ cannot be used as a function.
我的问题如下:
- 我在这段代码中做错了什么?
- 如何制作对象指针?
- 如何制作指向类的成员函数的指针以及如何使用它们?
请解释一下这些概念。
【问题讨论】:
标签: c++ member-function-pointers