Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

In need of reading material

25 views
Skip to first unread message

Doug Mika

unread,
Apr 30, 2015, 5:15:30 PM4/30/15
to
Hi to all

I have read much about passing parameters and objects by value and reference, and I think I understand all these concepts. The thing that is providing me with confusion is passing "functions" as parameters, be it by reference or by value. My books don't cover this and I don't quite know what phrase to search to find info on this. Does anyone know of any sites on the net that cover how functions are passed as parameters? After all they don't exist the same way in memory, or do they? Do we have such a thing as instances of functions?

Thanks
Doug

Victor Bazarov

unread,
Apr 30, 2015, 5:29:19 PM4/30/15
to
On 4/30/2015 5:15 PM, Doug Mika wrote:
> I have read much about passing parameters and objects by value and
reference, and I think I understand all these concepts. The thing that
is providing me with confusion is passing "functions" as parameters, be
it by reference or by value. My books don't cover this and I don't quite
know what phrase to search to find info on this. Does anyone know of any
sites on the net that cover how functions are passed as parameters?

I don't, sorry.

After all they don't exist the same way in memory, or do they? Do we
have such a thing as instances of functions?

You can think that functions are passed around as pointers, and functors
(objects of classes that define their own op() member function) are
passed around like all other objects. Look at a pointer to the function
as the address in the computer memory of the first machine instruction
into which that function code is translated. In C++ functions (pointers
to them) also have types, so you can't pass a function with one set of
argument types where a function with a different set is expected, but
there are sometimes conversions.

Perhaps "Inside the C++ Object Model" by Lippman contains more on that
and other topics. Check it out.

V
--
I do not respond to top-posted replies, please don't ask

Paavo Helde

unread,
Apr 30, 2015, 6:13:40 PM4/30/15
to
Doug Mika <doug...@gmail.com> wrote in
news:e9df6438-9207-4b03...@googlegroups.com:

> Hi to all
>
> I have read much about passing parameters and objects by value and
> reference, and I think I understand all these concepts. The thing
> that is providing me with confusion is passing "functions" as
> parameters, be it by reference or by value. My books don't cover this
> and I don't quite know what phrase to search to find info on this.
> Does anyone know of any sites on the net that cover how functions are
> passed as parameters?

Not really. The syntactis rules are quite confusing and any tutorial
attempting to cover them would be confusing as well. The good thing is
that you rarely need passing functions in C++ (virtual functions and
functors are better for many usage cases) and when you need them you can
use only a relatively small clear subset of the syntax - function
pointers.

See, in any case what is actually passed around is a pointer to the
function, not the function itself, whatever that would mean. Effectively,
functions are never passed "by value". C++ is not LISP. So one needs to
deal just with function pointers, and these are small simple objects
similar to data pointers. Pointers are naturally passed by value, and the
pointed functions themselves stay whereever they are.

> After all they don't exist the same way in
> memory, or do they? Do we have such a thing as instances of
> functions?

No. There is something similar - instantiation of templates, which can
produce multiple functions, but this happens at compile time and at
runtime these would be all different unique nonchanging and non-moving
functions (*).

(*) In an ideal world. In practice I have had a bug where a static
library was linked into different dynamic libraries so there were
multiple instances of the same functions present in the process. This was
all fine until a memory corruption bug ruined one of those copies. The
result was that when a certain function was called via one dll everything
worked fine, but if the same function with the same arguments was called
through another dll all hell broke loose. So yes, in practice one can
have multiple different "instances" of a function. But this would be a
bug, not a feature.

Cheers
Paavo

Message has been deleted

Richard

unread,
Apr 30, 2015, 6:47:55 PM4/30/15
to
[Please do not mail me a copy of your followup]

Doug Mika <doug...@gmail.com> spake the secret code
<e9df6438-9207-4b03...@googlegroups.com> thusly:

>I have read much about passing parameters and objects by value and
>reference, and I think I understand all these concepts. The thing that
>is providing me with confusion is passing "functions" as parameters, be
>it by reference or by value.

When you pass the name of a function as a parameter, it is always an
implicit pointer to the function and takes the same amount of space on
the argument stack as any other pointer.

#include <iostream>

void f()
{
std::cout << "Hi!\n";
}

void g(void fn())
{
fn();
}

int main()
{
g(f);
}

prints "Hi!\n" when you run it.

The names of functions always implicitly decay to a pointer to the
function. There isn't anything other you can do with the name of a
function as a value other than take its address, either implicitly or
explicitly.

The syntax gets a little uglier for pointers to member functions, but
the idea is the same -- you can only take the address of a member
function and pass it as a pointer or a reference. A reference in C++
is mechanically equivalent to a pointer but has one important semantic
difference -- a reference can never be NULL, so you can use references
without checking whether or not they refer to something (unlike a
pointer).

If you're talking about "function objects" such as the things passed
to the C++ standard library algorithms like "for_each", those are
either plain old function pointers as discussed above, or they are
instances of classes that implement operator().

In the case of passing function objects (also called functors), they
are passed just like any other object.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
Message has been deleted
0 new messages