References and interesting declarations

A

Aguilar, James

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? 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. I'd rather just do it with a reference.
 
R

Rolf Magnus

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?

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.
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.

You cannot pass arrays to functions anyway, since arrays are not
copyable.
 
J

John Harrison

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? 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. I'd rather just do it with a reference.

The declaration is

Point (&lst)[]

but your reasoning is wrong. C++ never passes whole arrays to functions.
It always passes a pointer to the first element.

void func(Point[] lst)

is exactly the same as

void func(Point* lst)

The first form is just a pretence and only ends up confusing newbies.

So just declare your parameter as a pointer. And remember that you can use
subscripts on a pointer just like you can use them on an array.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top