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