Question

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?

Was it helpful?

Solution

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::array is identical and available for C++03), which is a copyable type and can be passed by value.

int i[] = { 1, 2, 3 }; // type is int[3]
int j[3] = i; // error: can't copy an array
// C style would be to let j unitialized and to copy the values of
// i individually:
// int j[3]; std::copy(i, i + 3, j);

std::array<int, 3> ii = {{ 1, 2, 3 }};
std::array<int, 3> jj = ii; // Okay

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.


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?

In what language would 'copying a function' be meaningful?

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 that i is 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.

OTHER TIPS

(1) Is it possible to pass a copy of the array (without explicitly making a copy of the array inside the called function)?

In C++ you can pass by reference:

int a[100];
foo(a);

Now foo() will be,

void foo (int (&arr)[100])
{
}

So here you don't make a copy of a, but still you can use it inside foo() with the same rules you use it outside.

Edit: If you want to pass an array by value without making an explicit copy of it then you can think of putting it into a struct wrapper and pass it:

struct Array { int a[100]; } myArr;
foo(myArr);
void foo(Array myArr);

(2) 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?

Again the same thing, you can pass by reference. As far as its usefulness is concerned, it can be as useful as function pointer in most situations. But that's not conventional in C++. .

void foo (int i) {}
typedef void (pf)(int);
void pass (pf &ptr)
{
  ptr(0);
}

int main ()
{
  pass(foo);
}

Edit: If you are asking about making a copy like above, then no it's not possible in the case of function. Because it's not useful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top