473,406 Members | 2,710 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,406 software developers and data experts.

What is this function object?

Could anyone explain this snippet for me?

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class PrintInt {
public:
void operator() (int elem) const {
cout << elem << ' ';
}
};

int main() {
vector<intcoll;
for (int i = 1; i <=9; ++i)
coll.push_back(i);

for_each (coll.begin(), coll.end(), PrintInt()); // I don't
understand here
}

for_each needs a function object, so if we are doing: PrintInt p, then
we call for_each(coll.begin(), coll.end(), p()); I will understand,
but what is this "PrintInt()" here? Is it a default constructor? Or
just a call to overloaded operator ()?

Thank you for your response.

Nov 5 '07 #1
2 1331
On 11 5 , 1 13 , Porkling <porkl...@yeah.netwrote:
Here,PrintInt(),is a function object yet.
It runs constructor first (doing nothing here) , and runs operator()
later.

On Mon, 05 Nov 2007 04:58:13 -0000, "webinfin...@gmail.com"

<webinfin...@gmail.comwrote:
Could anyone explain this snippet for me?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class PrintInt {
public:
void operator() (int elem) const {
cout << elem << ' ';
}
};
int main() {
vector<intcoll;
for (int i = 1; i <=9; ++i)
coll.push_back(i);
for_each (coll.begin(), coll.end(), PrintInt()); // I don't
understand here
}
for_each needs a function object, so if we are doing: PrintInt p, then
we call for_each(coll.begin(), coll.end(), p()); I will understand,
but what is this "PrintInt()" here? Is it a default constructor? Or
just a call to overloaded operator ()?
Thank you for your response.- -

- -
accurately, opeartor() is called inside for_each, not immediately
after default constructor

Nov 5 '07 #2
On 2007-11-04 23:58:13 -0500, "we*********@gmail.com"
<we*********@gmail.comsaid:
Could anyone explain this snippet for me?

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class PrintInt {
public:
void operator() (int elem) const {
cout << elem << ' ';
}
};

int main() {
vector<intcoll;
for (int i = 1; i <=9; ++i)
coll.push_back(i);

for_each (coll.begin(), coll.end(), PrintInt()); // I don't
understand here
}

for_each needs a function object, so if we are doing: PrintInt p, then
we call for_each(coll.begin(), coll.end(), p()); I will understand,
But that call would be wrong. The algorithm needs the object, not the
result of calling its operator(). So the correct call is
for_each(coll.begin(), coll.end(), p);
but what is this "PrintInt()" here? Is it a default constructor? Or
just a call to overloaded operator ()?
Yes, it's the default contructor. It creates an unnamed temporary
object, and for_each gets a copy of that object. for_each calls the
copy's operator() as needed.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 5 '07 #3

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

Similar topics

28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
3
by: Brett | last post by:
What is the following code doing? I see evt and event but what is the difference? <input type="radio" id="us_countryFlag1" name="us_country" onclick="togglePurDec(event)">Yes <script>...
10
by: InvisibleMan | last post by:
Hi, Thanks for any help in advance... Okay, I have the JS listed below that calls for the display of the (DIV) tag... cookie function not included, as don't feel its necessary but you'll get the...
13
by: Jerry Manner | last post by:
Hi I have a problem using the location.href("ProductL.htm") javascript code. Using this code on my system ( W98 SE, IE 6.0) is working correctly. But if a user has W2000 IE 5.0 it doesn't work....
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
9
by: pamelafluente | last post by:
Hi, I was "studying" the famous (public code) BusyBox. I see the instruction: var busyBox = new BusyBox as in var busyBox = new BusyBox("BusyBox1", "busyBox", 4, "gears_ani_", ".gif",...
5
by: Daz | last post by:
Hi everyone. My query is very straight forward (I think). What's the difference between someFunc.blah = function(){ ; } and
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
8
by: Viktor | last post by:
Can somebody give me an explanation what happened here (or point me to some docs)? Code: HMMM = None def w(fn): print 'fn:', id(fn) HMMM = fn
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...

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.