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

operator() -- not executed inside for_each()

P: n/a
Hello,

I'm new in this group and I don't know if my questions are too silly,
but I'm intermediate programmer and don't have enought experience.
Please be charitable.

I don't know how to write an operator() class to be properly executed
inside for_each()?

Maybe example will be more accurate:

#v+
#include <algorithm>
#include <iostream>
#include <list>

class Class1
{
private:
/* ... */
double last_value;
public:
Class1() : last_value(0) {}

double show_result() {
return last_value;
}

void operator()(double n) {
if (n > last_value)
last_value = n;
}
}; // class

using namespace std;

int main() {
Class1 functor;
list<int> List1;

for (int i=0; i<6; ++i)
List1.push_back(i);

for_each(List1.begin(), List1.end(), functor);

functor.show_result(); // (1)

return 0;
}
#v-

And the problem is, that (1) returns 0, and I want it to return 5.
Where is a mistake in my reasoning?
How should it be written?

Thanks,
Michal

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


P: n/a
Michal Wyrebski wrote:
Hello,

I'm new in this group and I don't know if my questions are too silly,
but I'm intermediate programmer and don't have enought experience.
Please be charitable.

I don't know how to write an operator() class to be properly executed
inside for_each()?

Maybe example will be more accurate:

#v+
#include <algorithm>
#include <iostream>
#include <list>

class Class1
{
private:
/* ... */
double last_value;
public:
Class1() : last_value(0) {}

double show_result() {
return last_value;
}

void operator()(double n) {
if (n > last_value)
last_value = n;
}
}; // class

using namespace std;

int main() {
Class1 functor;
list<int> List1;

for (int i=0; i<6; ++i)
List1.push_back(i);

for_each(List1.begin(), List1.end(), functor);

functor.show_result(); // (1)

return 0;
}
#v-

And the problem is, that (1) returns 0, and I want it to return 5.
Where is a mistake in my reasoning?
How should it be written?

Thanks,
Michal


You wrote functor pretty well. But the last argument of for_each is passed as value. So you cannot
expect the change in main. You need to capture the value that for_each returns.

write ::
functor = for_each(List1.begin(), List1.end(), functor);

Krishanu
Jul 23 '05 #2

P: n/a
Michal Wyrebski wrote:
I'm new in this group and I don't know if my questions are too silly,
but I'm intermediate programmer and don't have enought experience.
Please be charitable.

I don't know how to write an operator() class to be properly executed
inside for_each()?

Maybe example will be more accurate:

#v+
#include <algorithm>
#include <iostream>
#include <list>

class Class1
{
private:
/* ... */
double last_value;
public:
Class1() : last_value(0) {}

double show_result() {
return last_value;
}

void operator()(double n) {
if (n > last_value)
last_value = n;
}
}; // class

using namespace std;

int main() {
Class1 functor;
list<int> List1;

for (int i=0; i<6; ++i)
List1.push_back(i);

for_each(List1.begin(), List1.end(), functor);

functor.show_result(); // (1)

return 0;
}
#v-

And the problem is, that (1) returns 0, and I want it to return 5.
Where is a mistake in my reasoning?
'for_each' makes a copy of the functor.
How should it be written?


You need some kind of indirection.

class Class1
{
private:
/* ... */
double& last_value; /// reference!!!
public:
Class1(double& d) : last_value(d) {}

double show_result() {
return last_value;
}

void operator()(double n) {
if (n > last_value)
last_value = n;
}
}; // class
...
double a = 0;
Class1 functor(a);
for_each(List1.begin(), List1.end(), functor);

Now, 'functor' keeps the reference to the place where it stores the
last value, and the reference survives the copying of 'functor'.

V
Jul 23 '05 #3

P: n/a

Krishanu Debnath wrote:
write ::
functor = for_each(List1.begin(), List1.end(), functor);


This works very well, but I have some doubt.

Does this solutions work ever, even if operator() is overloaded?
Could for_each() work on several copies of functor?
What if some elements are not maintained by one copy of functor?

Solution provided by Victor Bazarov is great and also may be used, but
in my case it's undesirable to have another variable (double a;).

Thanks.
Michal

Jul 23 '05 #4

P: n/a
Michal Wyrebski wrote:
Krishanu Debnath wrote:

write ::
functor = for_each(List1.begin(), List1.end(), functor);
This works very well, but I have some doubt.

Does this solutions work ever, even if operator() is overloaded?


Yes, as long as one of your () operator suitable match with your container's
element type.
Could for_each() work on several copies of functor?
What if some elements are not maintained by one copy of functor?


I didn't understand your above two questions.

Krishanu
Jul 23 '05 #5

P: n/a

Krishanu Debnath wrote:
Yes, as long as one of your () operator suitable match with your container's
element type.
Could for_each() work on several copies of functor?
What if some elements are not maintained by one copy of functor?

I didn't understand your above two questions.


Neah, sorry. I'm semiconscious today.

Now those questions after a mug of coffee:

Is it possible that for_each() works on several copies of functor, ex.
operator() is overloaded, and for_each() provides several types of data
to functor, so for_each() creates several copies of functor, and we
don't know which would be returned.

Someone's told me that it is not told that all elements will be
maintained by the same copy of functor.

And I'm not convinced about it, because how elements of list could be
more than one type when we have to declare the type of list.
Michal

Jul 23 '05 #6

P: n/a
Michal Wyrebski wrote:
Krishanu Debnath wrote:
Yes, as long as one of your () operator suitable match with your container's
element type.


Could for_each() work on several copies of functor?
What if some elements are not maintained by one copy of functor?


I didn't understand your above two questions.

Neah, sorry. I'm semiconscious today.

Now those questions after a mug of coffee:

Is it possible that for_each() works on several copies of functor, ex.
operator() is overloaded, and for_each() provides several types of data
to functor, so for_each() creates several copies of functor, and we


No, in your hypothetical situtation for_each can call different version of
operator(). There is *exactly one functor object* in for_each function.

But this sort of situtation cannot arise, as container can contain only one
type of data.

Krishanu
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.