By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

How to bind class static member function?

P: n/a
i use a class static member function to for_each:
class t1
{
static test(string s);
}

main()
{
list<string> slist;
...
for_each(slist.begin(), slist.end(), t1::test);
}
this can run, but when i give t1::test a parameter,like this :
class t1
{
static test2(string s, int 5);
}
main()
{
for_each(slist.begin(), slist.end(), bind2nd(t1::test2,5) ); //error !!!
for_each(slist.begin(), slist.end(), bind2nd(ptr_fun(t1::test2,5) ) );
//also error !!!
}

how to resolve this problem?

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
flyaflya wrote:
i use a class static member function to for_each:
class t1
{
static test(string s);
}

main()
{
list<string> slist;
...
for_each(slist.begin(), slist.end(), t1::test);
}
this can run, but when i give t1::test a parameter,like this :
class t1
{
static test2(string s, int 5);
}
main()
{
for_each(slist.begin(), slist.end(), bind2nd(t1::test2,5) );
//error !!! for_each(slist.begin(), slist.end(),
bind2nd(ptr_fun(t1::test2,5) ) ); //also error !!!
The closing parenthesis is in the wrong place.
}

how to resolve this problem?


Use 'bind2nd(ptr_fun(t1::test2),5)' as the third argument to 'for_each'.
-----------------------------
struct A {
static void foo(double,int);
};

#include <list>
#include <functional>
#include <algorithm>
#include <utility>

int main() {
std::list<double> d;
std::for_each(d.begin(), d.end(),
std::bind2nd(std::ptr_fun(A::foo), 5));
}
------------------------------
V
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.