Re: passing function object pointer to for_each argument
In article <1190880439.757760.323040@n39g2000hsh.googlegroups.com>,
hongseok.yoon@gmail.com wrote:
class test_a
{
public:
virtual void operator()(int i)
{
_test();
}
virtual void _test()
{
cout << "test_a::_test()" << endl;
}
};
class test_b : public test_a
{
public:
virtual void _test()
{
cout << "test_b::_test()" << endl;
}
};
int main()
{
test_a* a = new test_a;
test_a* b = new test_b;
vector<int> v;
v.push_back(1);
for_each(v.begin(), v.end(), *a);
for_each(v.begin(), v.end(), *b);
delete a;
delete b;
return 0;
}
Result :
test_a::_test()
test_a::_test()
------------------------------------------------
Why the result is not...
test_a::_test()
test_b::_test()
???
tell me why and how can I fix it?
for_each handles it's functor by value. In the second case, the value of
*b is copied into a test_a object that is stored inside the for_each
algorithm. To fix it, you need to add a level of indirection.
class test_t {
public:
virtual void test(int) = 0;
};
class test_a : public test_t {
void test(int) {
cout << "test_a::_test()\n";
}
};
class test_b : public test_t {
void test(int) {
cout << "test_b::_test()\n";
}
};
int main()
{
auto_ptr<test_t> a( new test_a );
auto_ptr<test_t> b( new test_b );
vector<int> v;
v.push_back( 1 );
for_each( v.begin(), v.end(),
bind1st( mem_fun( &test_t::test ), a.get() ) );
for_each( v.begin(), v.end(),
bind1st( mem_fun( &test_t::test ), b.get() ) );
return 0;
}
In the above code, I modified the test function somewhat so I could use
some of the functional parts of the language to build a wrapper, but you
can build one manually if you want:
class wrapper {
test_t* test;
public:
wrapper( test_t* t ): test( t ) { }
void operator()( int i ) {
test->test(); // assumes that '_test' takes no args like your
// example.
}
};
for_each( v.begin(), v.end(), wrapper( a.get() ) );
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]