VC6里无法为STL的for_eash绑定函数内部类,貌似因为for_eash用到了模板, 而当时版本的模块只能支持全局类。
解决如下:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define AUTO_CLASS_COUNT
template<class _T>
struct bass_fun
{
virtual void operator()(_T& _p)
{
cout<<"old "<<_p<<endl;
}
};
template<class _T>
struct bind_fun
{
bind_fun(bass_fun<_T>* _fun)
{
m_fun = _fun;
}
virtual void operator()(_T& _p)
{
(*m_fun)(_p);
}
bass_fun<_T> *m_fun;
};
void main()
{
vector<int> m_v_viewboxmgr;
for(int i=0; i<10; i++)
m_v_viewboxmgr.push_back(i);
struct Fun:public bass_fun<int>
{
virtual void operator()(int& _p)
{
cout<<"new "<<_p<<endl;
}
};
Fun fun;
for_each(m_v_viewboxmgr.begin(), m_v_viewboxmgr.end(), bind_fun<int>(&fun));
system("pause");
}
通过两个全局类和函数的多态机制绕开模板只支持全局类的问题。