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

The purpose of references to functions

P: n/a
Could someone elaborate on the purpose and concrete usage scenarios of
references to functions. What can references to functions do that
pointers to functions cannot. Why are they in C++?

A swap function with two reference parameters of the same type is a
typical example to explain the purpose of references to objects in C++
and the way they can be used, but since functions cannot be
"changed" (in the sense "exchange the bodies of functions foo and
bar"), I cannot think of an example making the purpose of references
to functions obvious. I guess I must be missing something.

I found a comment by Rob Williscroft in the Thread
http://groups.google.com/group/comp....736d2133cf30dc
saying:
Yes, but you could also ask another question, why are references to
functions part of C++ since they don't provide any utility that
isn't provided by function pointers. I think the answer is that
it allows a simple function to behave like a 'functor', particularly
a 'reference to a functor', more efficiently. This is important for
the standard library algorithms.
but I cannot not "see through" this exaplanation, why would a
reference to a function be more efficient, is this just a speculation?

-- Irfy
Mar 30 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.