简单地说,如果一个类重载了调用操作符(),那么这个类的对象就是所谓的函数对象。
一、 关于操作符重载的简单说明
1. 调用操作符()的重载必须以成员函数的形式出现,另外还必须以成员函数形式重载的操作符有:=、[ ]和->
2. IO操作符<<和>>的重载则必须以友元函数的形式出现。
3. 下面4个符号是不能被重载的:
::、 .*、 . 和 ?:
二、 for_each函数的简单说明
0. for_each在<algorithm>中定义
1. 函数原型:
template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function f);
2. 参数:
first, last
Input iterators to the initial and final positions in a sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
f
Unary function taking an element in the range as argument. This can either be a pointer to a function or an object whose class overloads operator().
Its return value, if any, is ignored.
3. 返回值:
和f的返回值一样
三、 示例代码
// 函数对象和for_each的用法
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
class Student
{
public:
Student(const string name, const int age) : name(name), age(age)
{}
//~Student()
//{
// cout << name << "\tdeleted." << endl;
//}
public:
const string get_name() const
{
return name;
}
const int get_age() const
{
return age;
}
private:
string name;
int age;
};
// 普通函数。global callable function
template<typename T>
void myfun(const T stu)
{
cout << "Name: " << stu.get_name() << "\tage: " << stu.get_age() << endl;
}
// 类myclass的对象就是一个函数对象,因为myclass重载了调用操作符()
template<typename T>
class myclass
{
public:
void operator()(const T);
};
template<typename T>
void myclass<T>::operator ()(const T stu )
{
cout << "Name: " << stu.get_name() << "\tage: " << stu.get_age() << endl;
}
int main(void)
{
vector<Student> myvector;
Student stu01("Andrew", 7);
Student stu02("Bob", 7);
Student stu03("Chris", 8);
Student stu04("Dudley", 7);
Student stu05("Ely", 8);
Student stu06("Flora", 9);
Student stu07("Greg", 8);
Student stu08("Howard", 9);
Student stu09("Iris", 10);
myvector.push_back(stu01);
myvector.push_back(stu02);
myvector.push_back(stu03);
myvector.push_back(stu04);
myvector.push_back(stu05);
myvector.push_back(stu06);
myvector.push_back(stu07);
myvector.push_back(stu08);
myvector.push_back(stu09);
// use the general function
cout << "myvector contains: " << endl;
for_each(myvector.begin(), myvector.end(), myfun<Student>);
cout << "--------------------------------" << endl;
// or use the function object
myclass<Student> mc;
cout << "myvector contains: " << endl;
for_each(myvector.begin(), myvector.end(), mc);
cout << "--------------------------------" << endl;
// 调用函数myfun
myfun<Student>(stu01);
// 调用函数对象mc,和调用普通函数一样
mc(stu01);
return 0;
}
运行结果:
myvector contains:
Name: Andrew age: 7
Name: Bob age: 7
Name: Chris age: 8
Name: Dudley age: 7
Name: Ely age: 8
Name: Flora age: 9
Name: Greg age: 8
Name: Howard age: 9
Name: Iris age: 10
--------------------------------
myvector contains:
Name: Andrew age: 7
Name: Bob age: 7
Name: Chris age: 8
Name: Dudley age: 7
Name: Ely age: 8
Name: Flora age: 9
Name: Greg age: 8
Name: Howard age: 9
Name: Iris age:10
--------------------------------
Name: Andrew age: 7
Name: Andrew age: 7