类b中有一个函数要用for_each进行一些操作,为了不单独写一个类或函数(仅b类使用)进行了以下试验,记录如下以防忘记
在gcc4.4下编译通过,VC6时类a需要提出来。
#include <functional>
#include <algorithm>
class b
{
public:
class a
{
public:
static bool test(int n, b* pb)
{
printf("%d/n", n+pb->m_1+pb->m_2);
return true;
}
};
public:
void mm()
{
m_1 = 100;
m_2 = 1000;
int arr[] = {0,1,2,3,4,5,6,7,8,9};
std::for_each(arr, arr+10, std::bind2nd(std::ptr_fun(b::a::test), this));
//也许VC能过
//std::for_each(arr, arr+10, std::bind2nd(std::ptr_fun(b::a::test), this));
}
private:
int m_1;
int m_2;
};
int main(int argc, char* argv[])
{
b().mm();
return 0;
}