mem_fun
和mem_fun_ref
许多其他成员函数适配器可以使成员函数像普通函数一样工作。但是有一个限制,他们调用的成员函数必须是const的。我开始知道如何使用它们,但对其背后的原因感到困惑和困惑。为什么要这样设计?
更新:抱歉含糊不清。下面写一个例子。
class A
{
...
//void fun(){cout<<"Fun";} This is not const and the compiler would complain
void fun() const {cout<<"Not fun";}
...
}
vector<A> avec;
...
for_each(avec.begin(),avec.end(),mem_fun_ref(&A::fun));
...