function adaptors for member functions

M

Mark A. Gibbs

good day,

i'm not sure if i'm using the standard function adaptors correctly. i
want to use a member function in a call to transform(), but not a member
function of the class being transformed.

i have:

class B {};
class C {};

class A
{
public:
virtual C foo(B);
};

C A::foo(B)
{
return C();
}

A const& a = /*initialized somehow, so may be a child of A*/;
vector<B> in(42, B());
vector<C> out(in.length(), C());

transform(in.begin(), in.end(), out.begin(), /*what do i do here?*/);

i want to call a.foo() for each member of in. i was thinking of:

bind1st(ptr_fun(&A::foo), &a);

or something like that, but i don't think that's kosher. there must be a
simple way to do this. i could write a helper function object to do it,
but i was wondering if there is already a way?

--
Mark A. Gibbs (aka. Indi)
Administrator
#c++ on irc.Rizon.net

http://ca.geocities.com/[email protected]/
(temporary website)
 
D

Donovan Rebbechi

i want to call a.foo() for each member of in. i was thinking of:

bind1st(ptr_fun(&A::foo), &a);

or something like that, but i don't think that's kosher. there must be a
simple way to do this. i could write a helper function object to do it,
but i was wondering if there is already a way?

try mem_fun_ref instead of ptr_fun.

Cheers,
 
I

Ioannis Vranos

Mark said:
good day,

i'm not sure if i'm using the standard function adaptors correctly. i
want to use a member function in a call to transform(), but not a member
function of the class being transformed.

i have:

class B {};
class C {};

class A
{
public:
virtual C foo(B);
};

C A::foo(B)
{
return C();
}

A const& a = /*initialized somehow, so may be a child of A*/;
vector<B> in(42, B());

B() is not needed here.

vector<C> out(in.length(), C());

length() does not exist. C() is not needed here. The above corrected:

transform(in.begin(), in.end(), out.begin(), /*what do i do here?*/);

i want to call a.foo() for each member of in. i was thinking of:

bind1st(ptr_fun(&A::foo), &a);

or something like that, but i don't think that's kosher. there must be a
simple way to do this. i could write a helper function object to do it,
but i was wondering if there is already a way?


The only way I can think, is to create a regular function and use it.
Your code fixed and made to compile in the function way:


#include <vector>
#include <algorithm>
#include <functional>


class B {};
class C {};

class A
{
public:
virtual C foo(const B &) const { return C(); }
};


inline C MakeC (const A objA, const B objB)
{
return objA.foo(objB);
}





int main()
{
using namespace std;

A obj;
A &a = obj;

vector<B> in(42);

vector<C> out(in.size());

transform(in.begin(), in.end(), out.begin(),
bind1st(ptr_fun(MakeC), a));
}



Keep in mind that it doesn't compile to VC++ 7.1/8 Beta due to a library
bug.
 
M

Mark A. Gibbs

Ioannis said:
The only way I can think, is to create a regular function and use it.
Your code fixed and made to compile in the function way:

sorry for the messy code, the original used lots of peculiar types and
stuff, so i rewrote it in place just to give you the gist of the problem.

that solution was what i figured too. i was trying to avoid the extra
helper function cause it means i'm gonna need like twenty of them. ah
well. thank you for your help.
Keep in mind that it doesn't compile to VC++ 7.1/8 Beta due to a library
bug.

not a problem - don't use it.

--
Mark A. Gibbs (aka. Indi)
Administrator
#c++ on irc.Rizon.net

http://ca.geocities.com/[email protected]/
(temporary website)
 
I

Ioannis Vranos

Mark said:
sorry for the messy code, the original used lots of peculiar types and
stuff, so i rewrote it in place just to give you the gist of the problem.

that solution was what i figured too. i was trying to avoid the extra
helper function cause it means i'm gonna need like twenty of them. ah
well. thank you for your help.


In the meantime your code was improved in the discussion thread with the
subject "mem_fun vs mem_fun_ref".


Here is the improved version working with the member function directly:


#include <vector>
#include <algorithm>
#include <functional>


class B {};
class C {};

class A
{
public:
virtual C foo(const B) const { return C(); }
};



int main()
{
using namespace std;

A obj;
A &a = obj;

vector<B> in(42);

vector<C> out(in.size());

transform(in.begin(), in.end(), out.begin(),
bind1st(mem_fun_ref(&A::foo), a) );
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
FloridaHan

Latest Threads

Top