Note: I am using the g++ compiler (which is I hear is pretty good and supposed to be pretty close to the standard).
So, I think I’ve learned that passing a pointer-to-an-array or passing the actual array as an argument to another function always results in the passing of a pointer-to-that-array.
Similarly, I think I’ve learned that passing a pointer-to-a-function or passing the actual function as an argument to another function always results in the passing of a pointer-to-that-function.
Is it possible to pass a copy of the array (without explicitly making a copy of the array inside the called function)?
Is it possible to pass a copy of the function (without explicitly making a copy of the function inside the called function)? Is that ever useful? What does that even mean?
I understand the difference between passing an array or its copy; what is the difference between passing a function or its copy?
C doesn’t make it possible to pass arrays by value or more generally to copy them (unless by hand). C++ inherits that, so no it is not possible. The better approach would be to use
std::array(for C++0x;boost::arrayis identical and available for C++03), which is a copyable type and can be passed by value.It is not possible in either languages to copy functions and unlike arrays it is not possible to do it ‘by hand’. ‘Copying a function’ is not meaningful in those languages.
Lua allows reading the byte-code of a function and it can be reused at a later point (or in another interpreter altogether) to reconstitute a function. I also assume that Lisp, under appropriate circumstances, can do the same with its code-as-data approach. Note that in both those languages it would not be common to do that and it’s not idiomatic to call it ‘copying a function’.
This sort of thing is less copying in the
int i = j;sense thatiis a copy, but is usually closer to the concept of marshalling/serialization. That is to say, the transformation of runtime data* into a more persistent form that can be stored and/or transmitted (e.g. across a network).*: in those languages (among others) and unlike in C and C++, functions are data.