By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,222 Members | 1,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,222 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Timothy Madden wrote in news:2v*************@uni-berlin.de 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

P: n/a

"Timothy Madden" <ba****@rmv.spam.home.ro> wrote in message
news:2v*************@uni-berlin.de...
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
Jul 22 '05 #3

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2v*************@uni-berlin.de...

"Timothy Madden" <ba****@rmv.spam.home.ro> wrote
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
Jul 22 '05 #4

P: n/a

"Timothy Madden" <ba****@rmv.spam.home.ro> wrote in message
news:2v*************@uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2v*************@uni-berlin.de...

"Timothy Madden" <ba****@rmv.spam.home.ro> wrote
> 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
Jul 22 '05 #5

P: n/a
> > 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

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.
Jul 22 '05 #6

P: n/a

"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
news:41******@news.greennet.net...
> 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

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

Jul 22 '05 #7

P: n/a

"Howard" <al*****@hotmail.com> wrote in message
news:nT*******************@bgtnsc05-news.ops.worldnet.att.net...

"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
news:41******@news.greennet.net...
> 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

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


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
Jul 22 '05 #8

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2v*************@uni-berlin.de...

"Howard" <al*****@hotmail.com> wrote in message
news:nT*******************@bgtnsc05-news.ops.worldnet.att.net...

"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
news:41******@news.greennet.net...
> 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
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).


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
Jul 22 '05 #9

P: n/a
"Timothy Madden" <ba****@rmv.spam.home.ro> wrote in message news:<2v*************@uni-berlin.de>...
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
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.