Login
[C++] Иерархия функторов
358 просмотров
Перейти к просмотру всей ветки
in Antwort voxel3d 21.11.06 00:18
Ты имеешь в виду, что п-м не работает? Так работает же.
#ifndef __TEST_CPP__
#define __TEST_CPP__
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
class Base : public unary_function<int, void>
{
public:
Base() {}
Base(const Base& base)
{
n_copied++;
}
void operator()(int num)
{
cout << num_op(num) << " ";
}
Base& operator=(const Base& base)
{
n_assigned++;
}
static int n_copied;
static int n_assigned;
private:
virtual int num_op(int num)
{
return num;
}
};
int Base::n_copied = 0;
int Base::n_assigned = 0;
class Derived : public Base
{
public:
Derived() {}
Derived(const Derived& d)
{
n_copied++;
}
private:
virtual int num_op(int num)
{
return num*num;
}
};
int main(int argc, char* argv[])
{
vector<int> coll;
for (int i = 0; i < 10; i++)
coll.push_back(i);
Base b;
Derived d;
cout << "n_copied = " << Base::n_copied << endl;
for_each<vector<int>::iterator, Base&>(coll.begin(), coll.end(), b);
cout << endl;
cout << "n_copied = " << Base::n_copied << endl;
for_each<vector<int>::iterator, Base&>(coll.begin(), coll.end(), d);
cout << endl;
cout << "n_copied = " << Derived::n_copied << endl;
return 0;
}
#endif