您的位置:首页 > 其它

解决VC6无法为STL的for_eash绑定函数内部类的问题

2012-07-10 23:48 519 查看
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");

}


通过两个全局类和函数的多态机制绕开模板只支持全局类的问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐