本文参考MSDN,介绍()操作符函数调用
class MultValue
{
int mnFactor;
public:
MultValue(int lsh)
:mnFactor(lsh)
{
}
void operator ( ) ( int& elem ) const
{
elem *= mnFactor;
}
};
int main ()
{
int elems = 3;
MultValue mval(10); //对应汇编调用 MultValue::MultValue (411028h)
mval(elems); //调用()操作符 elems = 3*10, 汇编调用MultValue::operator() (41119Ah)
MultValue(20)(elems); //构造函数和operator()函数的结合 elems = 30*20
return 0;
}
一般使用场景是在for_each语句中,例如
vector <int> v1;
...
for_each ( v1.begin ( ) , v1.end ( ) , MultValue(6) );
msdn: ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_vcstdlib/html/8cb2ae72-bef6-488b-b011-0475c0787e33.htm