473,769 Members | 6,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator() -- not executed inside for_each()

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()(doub le 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_re sult(); // (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
6 1550
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()(doub le 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_re sult(); // (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
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()(doub le 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_re sult(); // (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()(doub le 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

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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1968
by: BCC | last post by:
In looking through some code I have inherited, I notice a lot of places where the programmer used operator() as a function call: void operator() (int x, int y); Rather than an explicit function name: void MyFunction(int x, int y); Then when he instantiates a class he calls it: MyClass myclass; myclass (x, y);
2
1207
by: Malcolm Smith | last post by:
I have this functor: struct DeleteIterObject { template< typename T > void operator()(const T* ptr) const { delete ptr; ptr = NULL; }
4
1796
by: Jef Driesen | last post by:
Is there an STL algorithm that does something like for (iterator i = begin(); i != end(); ++i) f(*i,value); where f results in the expression *i += value. I already found the algorithm 'transform' with the 'plus' function object, but it does evaluate to *i = *i + value. And this is not exactly what I want.
21
3046
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as opposed to what is mentioned in precedence table? Also, with comma operator. consider,
7
5127
by: Bas Nedermeijer | last post by:
Hello, i am having trouble to use the sort() function of a list<>. I cannot seem to overload the operator<(). The variables to compare are references, like Item* itemA; Item* itemB;
3
1648
by: Nindi | last post by:
On comp.lang.c++.moderated http://groups.google.co.uk/group/comp.lang.c++.moderated/browse_thread/thread/8250715711da7760?hl=en the following question was posted ------------------------------------------------------------------------------------------------------- Is this standard conformant to type cast ::opearator delete to a function of type (void (*)(void*)); Is not operator delete already a function of this type?
7
2423
by: Deron Meranda | last post by:
This is an optimization problem, one that occurs deep inside a loop that gets executed thousands of times. I'm looking for something in Python which would act like an identity operator, or I-combinator: a do-nothing function. The best I know of is: (lambda x: x), but it is not ideal. Consider a much-simplified example of such an iteration where sometimes you need to transform an item by some function, but most of the time you don't:
3
1742
by: George2 | last post by:
Hello everyone, I can not find related information in MSDN, so I want to confirm here, that, the implementation of for_each is something like, template <class A, class BB for_each (A begin, A end, B Func)
3
3520
by: C++Liliput | last post by:
Hi, I was looking at the implementation of operator new and operator new in gcc source code and found that the implementation is exactly the same. The only difference is that the size_t argument passed to the operators is calculated correctly during runtime and passed in. Inside the implementation both operators (new and new) do a simple malloc(). Ditto for operator delete/delete. I have two questions: 1) Who passes in the size_t argument...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.