还是函数指针和指针,帮忙看看
//代码
class X{
typedef void(X::*func)(int**);
private:
int count;
public:
//构造函数
X(){ count = 0; };
//用函数指针作参数的函数
void call(func pt_func , int* buff[] ){
(this->*pt_func)( buff );
if( count < 3 ) call( (this->*pt_func) , buff);
};
//函数指针所指的函数
void modify( int * buff[]) {
count ++;
cout<<"modify data "<<endl;
*(buff[0]) = count;
*(buff[1]) = count + 1;
};
//调用有函数指针参数的函数
void test(){
func pt_func = &X::modify;
int a1 = 0;
int a2 = 0;
int * buff[2] = {&a1, &a2};
call( pt_func, buff );
cout<<"data:"<<endl
<<*(buff[0])<<" "
<<*(buff[1])<<" "
<<endl;
};
};
//主函数
int main(int argc, char *argv[])
{
X x;
x.test();
system("PAUSE");
return 0;
}
//说明
我的本意是想在test下面开辟出来一个指针型buff,然后通过call调用modify修改这个buff里面指针所指的内容
//问题
1,编译完全通过
2,运行时出示指针方面的问题
3,调试发现第一次跑到modify的时候没有错误,buff修改正确。但第二次跑到这里的时候,buff指针指向错误的地方去了,于是出错。
4,为什么会这样?