
/**//*
*測試成員函數(shù)指針數(shù)組的小程序
*/
#include <iostream>
using namespace std;

class Test


{
public:
Test();
~Test();

private:

void add5()
{ res+=5;}

void add6()
{ res+=6;}

void (Test::*add[2])();//這個2至關(guān)重要,在VC下沒寫會報(bào)錯,但在QT里沒報(bào),但析構(gòu)時(shí)出錯!
public:
void DoAddAction();
void Display();
private:
int res;
};


Test::Test()


{
add[0]=&Test::add5;//注意這里的寫法
add[1]=&Test::add6;
res=0;
}

Test::~Test()


{

}

void Test::DoAddAction()


{
for (int i=0;i<2;i++)

{
(this->*add[i])();//使用類成員函數(shù)指針必須有“->*”或“.*”的調(diào)用
}
}

void Test::Display()


{
cout<<"The res is:"<<res<<endl;
}

int main()


{
Test * test=new Test();
test->DoAddAction();
test->Display();
delete test;
return 0;
}