| re: References and interesting declarations
Aguilar, James wrote:
[color=blue]
> Hey all. Another question that I'm having trouble with. I want to
> make something of a really complicated declaration (in this case,
> declaring
> parameters). Can you tell me how I would write this? I have a type
> called "Point" and I want to pass a reference to an array of pointers
> to Points.
>
> Currently, the function signature is like this:
>
> PointPair bruteforce(int n, Point* lst[]);
>
> But I think it should be something like (Point* [])& lst) Is that at
> all right?[/color]
No. If you want a reference to an array, you have to specify a size,
since arrays always need a fixed size. It would look something like:
Point* (&lst)[42]
Looking at the first parameter of your function, I suspect you don't
want a fixed size, but rather specify the size though that parameter,
in which case a reference to an array is not the right tool for the
job. In this case, just pass a pointer to the first element of your
array:
PointPair bruteforce(int n, Point** lst);
Better would even be to not use arrays in the first place and rather use
a vector, which keeps track of its size on its own.
[color=blue]
> What is right? The reason why is because I don't want to pass
> the whole honking array, cause it's going to be on the order of ten
> thousand elements.[/color]
You cannot pass arrays to functions anyway, since arrays are not
copyable.
[color=blue]
> I'd rather just do it with a reference.[/color] |