473,385 Members | 1,185 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,385 software developers and data experts.

for_each with a non const member fuction

Hi Everyone

So, I want to use a std::for_each in the following situation:

class B{
//whatever
}

class A{
private:
void configure_A_from_B(const std::vector<B*>& bPtrVec);
void member_function(B*); //non const.
}

A::configure_A_from_B(const std::vector<B*>& bPtrVec){
std::for_each( bPtrVec.begin(), bPtrVec.end(), member_function() )
}

i.e. I want to use std::for_each to call a member (of A) function,
with a single parameter, the pointer pointed to by the iterator I am
for_eaching over.

Unfortunatley I'm tripped up by the whole binding thing... can someone help
me out? I'm also willing to use the boost lambda libraries, but I haven't
been
able to sort this out.

Thanks,

Glen
Aug 16 '05 #1
5 2674
one way to use the for_each you need to create a function object. This
can be done by overriding the operator() of the class.

delare the class A as

class A{
private:
void configure_A_from_B(const std::vector<B*>& bPtrVec);
//void member_function(B*); //non const.
void operator()(const B* value);
}

A::operator()(const B* value)
{
//do what ever you want to do with B*
}

The function A::configure_A_from_B will be modified as :
A::configure_A_from_B(const std::vector<B*>& bPtrVec){
std::for_each( bPtrVec.begin(), bPtrVec.end(), A());
}

Aug 16 '05 #2

"glen stark" <st***@ife.ee.ethz.ch> wrote in message
news:43********@news1.ethz.ch...
Hi Everyone

i.e. I want to use std::for_each to call a member (of A) function,
with a single parameter, the pointer pointed to by the iterator I am
for_eaching over.

Unfortunatley I'm tripped up by the whole binding thing... can someone
help
me out? I'm also willing to use the boost lambda libraries, but I haven't
been
able to sort this out.
Use the boost bind library:

So, I want to use a std::for_each in the following situation:
#include <boost/bind.hpp>
class B{
//whatever
}

class A{
private:
void configure_A_from_B(const std::vector<B*>& bPtrVec);
void member_function(B*); //non const.
}

A::configure_A_from_B(const std::vector<B*>& bPtrVec){
std::for_each( bPtrVec.begin(), bPtrVec.end(), member_function() )
std::for_each( bPtrVec.begin()
, bPtrVec.end()
, boost::bind( &A::member_function, this, _1 )
);
}


Jeff Flinn
Aug 16 '05 #3

<ra************@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
one way to use the for_each you need to create a function object. This
can be done by overriding the operator() of the class.


Thanks for the suggestion.I forgot to mention, I don't want to create an
explicit function object. I run into this situation all the time, and it
seems to me that there must be a way to bind the member function (perhaps
using boost lambda libraries
to be able to do this in a quick clean way without having to create a
function object for every instance I want to do this.
Aug 16 '05 #4

glen stark wrote:
Hi Everyone

So, I want to use a std::for_each in the following situation:

class B{
//whatever
}

class A{
private:
void configure_A_from_B(const std::vector<B*>& bPtrVec);
void member_function(B*); //non const.
}

A::configure_A_from_B(const std::vector<B*>& bPtrVec){
std::for_each( bPtrVec.begin(), bPtrVec.end(), member_function() )
}

i.e. I want to use std::for_each to call a member (of A) function,
with a single parameter, the pointer pointed to by the iterator I am
for_eaching over.

Unfortunatley I'm tripped up by the whole binding thing... can someone help
me out? I'm also willing to use the boost lambda libraries, but I haven't
been
able to sort this out.


Do you mean boost::bind?

std::bind1st and std::mem_fun combination do not allow a non-const
member function so you'd have to try either boost::bind or write your
own function object.

boost::bind would work (I think) thus:

std::for_each( bPtrVec.begin(), bPtrVec.end(),
boost::bind( &A::member_function, this, _1 ) );

They're not very clear with their examples for this typical situation
(I don't know why).

Aug 16 '05 #5
> Use the boost bind library:

So, I want to use a std::for_each in the following situation:


#include <boost/bind.hpp>
class B{
//whatever
}

class A{
private:
void configure_A_from_B(const std::vector<B*>& bPtrVec);
void member_function(B*); //non const.
}

A::configure_A_from_B(const std::vector<B*>& bPtrVec){
std::for_each( bPtrVec.begin(), bPtrVec.end(), member_function() )


std::for_each( bPtrVec.begin()
, bPtrVec.end()
, boost::bind( &A::member_function, this, _1 )
);
}


Jeff Flinn

Thanks, that's just what I was looking for. I tried the bind library, but
didn't get the syntax right for
the member function (didn't have the 'this' argument).

glen
Aug 16 '05 #6

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

Similar topics

5
by: Alex Vinokur | last post by:
Functor-parameters in the for_each() and transform() algorithms are passed by value. Might it make sense to have also algorithms for_each2() and transform2() which pass Functor-parameters by...
1
by: Capstar | last post by:
Hi NG, I have a question on std::for_each. I try to use this in combination with std::bind2nd to call a method of all functions in a container (std::set or std::map) and pass that method the...
2
by: waitan | last post by:
#include <algorithm> #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> #include <iterator> #include <iomanip> using namespace std;
9
by: Chris Roth | last post by:
I have a vector of vectors: vector< vector<double v; and have initialized it with: v( 5 ); as I know I will have 5 columns of data. At this point, I read text file data into each of the the...
15
by: Eric Lilja | last post by:
Hello, in one of my classes I have this member variable: Tile ***tiles_; where Tile is another one of my classes. dimensional array of Tile-pointers (dimensions are of equal sizes). Last, I...
3
by: Yan | last post by:
Hi, I don't seem to be able to use for_each if it should replace a 'for' loop in a method (constructor in my case) and inside that 'for' loop a class member variable is being accessed. The...
4
by: Kenneth Porter | last post by:
I'm trying to create a class that represents a bucket of locks, and I want to lock a set of objects specified by a container of pointers. I can't seem to find the right combination of function...
9
by: nguillot | last post by:
Hello I used to loop on a std::map<k, dto act on the data (d) like that (d being a class with setIntMember method): typedef std::map<k, dtMap; struct setIntMember { setIntMember(int j) :...
8
by: Ruben | last post by:
error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers seems to be some sort of standard error format that I'm not understanding. I have code that...
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
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?

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.