473,378 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

function adaptors for member functions


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/in***@rogers.com/
(temporary website)

Jul 23 '05 #1
4 1308
On 2005-03-17, Mark A. Gibbs <x_*********@rogers.com_x> wrote:
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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #2
Mark A. Gibbs wrote:


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:

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


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.
--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #3

Ioannis Vranos wrote:
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/in***@rogers.com/
(temporary website)

Jul 23 '05 #4
Mark A. Gibbs wrote:
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) );
}


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: 0to60 | last post by:
I don't know if I have that terminology right, but does anyone know if static member functions (or free standing functions for that matter) are any less overhead than actual member functions that...
3
by: red floyd | last post by:
I have a suggestion for the standard library.... This is sort of a combination of std::transform() and std::for_each(). It applies the binary function to each iterator in ...
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
2
by: Drew McCormack | last post by:
I have a self written Tensor class which I need to write a number of elementwise operations for (eg sin, cos, abs, conj). I am trying to implement most of these in terms of standard library...
18
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
18
by: WaterWalk | last post by:
Hello. Suppose there is an implementation of C++, in which when a class object is allocated, its member functions are also allocated in addition to its data members. So that every class object has...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.