Why I can't have a reference to an unsized array ?

T

Timothy Madden

Hello

If I write a function like this
void Process(double Data[])
{ ... }
it is ok, but if I try
class DataProcess
{
double (&Data)[];
DataProcess(double Data[])
:Data(Data)
{ }
};

I get a compile error.
What's the difference between the argument array and the data-member array,
apart from syntax ?

Thank you
Timothy Madden
Romania
 
R

Rob Williscroft

Timothy Madden wrote in in
comp.lang.c++:
Hello

If I write a function like this
void Process(double Data[])
{ ... }
it is ok, but if I try
class DataProcess
{
double (&Data)[];
DataProcess(double Data[])
:Data(Data)
{ }
};

I get a compile error.
What's the difference between the argument array and the data-member
array, apart from syntax ?

The first is a way of passing a pointer, i.e:

void Process(double Data[])

is a actually the same function as:

void Process(double *Data)

Its a quirk of the language, try:

class DataProcess
{
double *Data;
DatProcess( double Data[] ) : Data( Data ) {}
};

HTH.

Rob.
 
J

John Harrison

Timothy Madden said:
Hello

If I write a function like this
void Process(double Data[])
{ ... }

There is no such thing an an unsized array in standard C++. The function you
wrote is just another way of writing

void Process(double* Data)
{ ... }

Data is a pointer, not an unsized array (which doesn't exist).
it is ok, but if I try
class DataProcess
{
double (&Data)[];
DataProcess(double Data[])
:Data(Data)
{ }
};

I get a compile error.
What's the difference between the argument array and the data-member
array,
apart from syntax ?

The argument array isn't an array, its a pointer. You cannot have an array
of any type as a parameter in C++, even if you give it a size.

void Process(double Data[10])

Data is still a pointer, the size is completely ignored. This syntax is
stupid, it only serves to confuse, but it has existed since the original C.

john
 
T

Timothy Madden

John Harrison said:
Timothy Madden said:
I try
class DataProcess
{
double (&Data)[];
DataProcess(double Data[])
:Data(Data)
{ }
};

I get a compile error.
What's the difference between the argument array and the data-member
array,
apart from syntax ?

The argument array isn't an array, its a pointer. You cannot have an array
of any type as a parameter in C++, even if you give it a size.

void Process(double Data[10])

Data is still a pointer, the size is completely ignored. This syntax is
stupid, it only serves to confuse, but it has existed since the original
C.

This makes sense. Thank you.

How about
extern double Data[];
?
Is this also quirck to declaring an extern pointer ?

Oh, and I have another question.

Why 'array of references is not allowed' ?. What's wrong with it ? There
have been times when I could use one ...

Thank you
Timothy Madden
Romania
 
J

John Harrison

Timothy Madden said:
John Harrison said:
Timothy Madden said:
I try
class DataProcess
{
double (&Data)[];
DataProcess(double Data[])
:Data(Data)
{ }
};

I get a compile error.
What's the difference between the argument array and the data-member
array,
apart from syntax ?

The argument array isn't an array, its a pointer. You cannot have an
array
of any type as a parameter in C++, even if you give it a size.

void Process(double Data[10])

Data is still a pointer, the size is completely ignored. This syntax is
stupid, it only serves to confuse, but it has existed since the original
C.

This makes sense. Thank you.

How about
extern double Data[];
?
Is this also quirck to declaring an extern pointer ?

No, in that case Data is an array. Like all arrays it's fixed size, you just
don't know what that size is.

Easy test to see if something is an array or a pointer

Data = 0;

If that compiles Data is a pointer, if it doesn't it's an array (I'm
ignoring the issue of const here).
Oh, and I have another question.

Why 'array of references is not allowed' ?. What's wrong with it ? There
have been times when I could use one ...

Good question, I'm not sure what the answer is. I would use an array of
pointers instead.

john
 
F

Fraser Ross

Oh, and I have another question.
Good question, I'm not sure what the answer is. I would use an array of
pointers instead.

john


It is illegal to have an array of references, because pointers to references
are not allowed and array names are coerced into pointers.

Thats the answer from BCB.

Fraser.
 
H

Howard

Fraser Ross said:
It is illegal to have an array of references, because pointers to
references
are not allowed and array names are coerced into pointers.

Thats the answer from BCB.

Fraser.

Also, how would you initialize such an array? You can't loop through it and
assign values to references, since you can only initialize references, not
assign to them. So there'd be no way to simply declare the array in the
first place, without a method of initializing the entire array at that point
(and a way to require that that initialization be performed).

-Howard
 
J

John Harrison

Howard said:
Also, how would you initialize such an array? You can't loop through it
and assign values to references, since you can only initialize references,
not assign to them. So there'd be no way to simply declare the array in
the first place, without a method of initializing the entire array at that
point (and a way to require that that initialization be performed).

Like this I guess

int a, b, c;
int& ref[3] = { a, b, c };

But the point about pointers to references and array names seems conclusive.

john
 
T

Timothy Madden

John Harrison said:
Howard said:
Also, how would you initialize such an array? You can't loop through it
and assign values to references, since you can only initialize references,
not assign to them. So there'd be no way to simply declare the array in
the first place, without a method of initializing the entire array at that
point (and a way to require that that initialization be performed).

Like this I guess

int a, b, c;
int& ref[3] = { a, b, c };

But the point about pointers to references and array names seems conclusive.

john
:((
Did I mention I don't really like this language ?
But it looks like it's the only one of its kind.
Thank you all anyway

Timothy Madden
Romania
 
M

Michiel Salters

Timothy Madden said:
Oh, and I have another question.

Why 'array of references is not allowed' ?. What's wrong with it ? There
have been times when I could use one ...

Try std::vector<boost::ref<T> >

Regards,
Michiel Salters
 

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,184
Messages
2,570,973
Members
47,527
Latest member
RoxanneTos

Latest Threads

Top