Why I can't have a reference to an unsized array ? 
July 22nd, 2005, 09:53 PM
| | | Why I can't have a reference to an unsized array ?
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 | 
July 22nd, 2005, 09:53 PM
| | | Re: Why I can't have a reference to an unsized array ?
Timothy Madden wrote in news:2vrdroF2o9kkmU1@uni-berlin.de in
comp.lang.c++:
[color=blue]
> 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 ?
>[/color]
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/ | 
July 22nd, 2005, 09:53 PM
| | | Re: Why I can't have a reference to an unsized array ?
"Timothy Madden" <batman@rmv.spam.home.ro> wrote in message
news:2vrdroF2o9kkmU1@uni-berlin.de...[color=blue]
> Hello
>
> If I write a function like this
> void Process(double Data[])
> { ... }[/color]
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).
[color=blue]
> 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 ?[/color]
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 | 
July 22nd, 2005, 09:53 PM
| | | Re: Why I can't have a reference to an unsized array ?
"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:2vrec5F2nm8fqU1@uni-berlin.de...[color=blue]
>
> "Timothy Madden" <batman@rmv.spam.home.ro> wrote[color=green]
> > 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 ?[/color]
>
> 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[/color]
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 | 
July 22nd, 2005, 09:54 PM
| | | Re: Why I can't have a reference to an unsized array ?
"Timothy Madden" <batman@rmv.spam.home.ro> wrote in message
news:2vrfj8F2ok160U1@uni-berlin.de...[color=blue]
>
> "John Harrison" <john_andronicus@hotmail.com> wrote in message
> news:2vrec5F2nm8fqU1@uni-berlin.de...[color=green]
>>
>> "Timothy Madden" <batman@rmv.spam.home.ro> wrote[color=darkred]
>> > 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 ?[/color]
>>
>> 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[/color]
> C.
>
> This makes sense. Thank you.
>
> How about
> extern double Data[];
> ?
> Is this also quirck to declaring an extern pointer ?[/color]
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).
[color=blue]
>
> 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 ...[/color]
Good question, I'm not sure what the answer is. I would use an array of
pointers instead.
john | 
July 22nd, 2005, 09:54 PM
| | | Re: Why I can't have a reference to an unsized array ?
> > Oh, and I have another question.[color=blue][color=green]
> >
> > Why 'array of references is not allowed' ?. What's wrong with it ? There
> > have been times when I could use one ...[/color]
>
> Good question, I'm not sure what the answer is. I would use an array of
> pointers instead.
>
> john[/color]
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. | 
July 22nd, 2005, 09:54 PM
| | | Re: Why I can't have a reference to an unsized array ?
"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
news:4198edd5@news.greennet.net...[color=blue][color=green][color=darkred]
>> > 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 ...[/color]
>>
>> Good question, I'm not sure what the answer is. I would use an array of
>> pointers instead.
>>
>> john[/color]
>
>
> 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.
>
>[/color]
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 | 
July 22nd, 2005, 09:54 PM
| | | Re: Why I can't have a reference to an unsized array ?
"Howard" <alicebt@hotmail.com> wrote in message
news:nT3md.22450$7i4.14030@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
>
> "Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
> news:4198edd5@news.greennet.net...[color=green][color=darkred]
>>> > 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[/color]
>>
>>
>> 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.
>>
>>[/color]
>
> 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).[/color]
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 | 
July 22nd, 2005, 09:54 PM
| | | Re: Why I can't have a reference to an unsized array ?
"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:2vs1n0F2mu2b1U1@uni-berlin.de...[color=blue]
>
> "Howard" <alicebt@hotmail.com> wrote in message
> news:nT3md.22450$7i4.14030@bgtnsc05-news.ops.worldnet.att.net...[color=green]
> >
> > "Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
> > news:4198edd5@news.greennet.net...[color=darkred]
> >>> > 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[/color][/color][/color]
of[color=blue][color=green][color=darkred]
> >>> 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.
> >>
> >>[/color]
> >
> > Also, how would you initialize such an array? You can't loop through it
> > and assign values to references, since you can only initialize[/color][/color]
references,[color=blue][color=green]
> > 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[/color][/color]
that[color=blue][color=green]
> > point (and a way to require that that initialization be performed).[/color]
>
> 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[/color]
conclusive.[color=blue]
>
> john
>[/color]
:((
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 | 
July 22nd, 2005, 09:56 PM
| | | Re: Why I can't have a reference to an unsized array ?
"Timothy Madden" <batman@rmv.spam.home.ro> wrote in message news:<2vrfj8F2ok160U1@uni-berlin.de>...[color=blue]
> 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 ...[/color]
Try std::vector<boost::ref<T> >
Regards,
Michiel Salters | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,840 network members.
|