Topic: Assigning to an array in one go?
Author: default@nospam.invalid ("Espen Ruud Schultz")
Date: Sun, 11 May 2003 19:23:01 +0000 (UTC) Raw View
Now why isn't this legal: ?
int Test5i[ 5 ];
Test5i = { 1, 2, 3, 4, 5 };
Maybe it should have a different syntax though:
Test5i[] = { 1, 2, 3, 4, 5 }; or:
Test5i[*] = { 1, 2, 3, 4, 5 };
But still, the compiler would have no problem understanding what's going on.
One could even do this:
Test5i[2] = { 3, 4, 5 };
And the compiler would understand that the values should be assigned from
element 2 and out. This would be a nice way to cut down on lines when
assigning values to an array. I can't see why this isn't legal. I know the
compiler would have no problem at all. It would also give 100% correct
error values, so I don't see this as a pitfall...
, Espen
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: ron@sensor.com ("Ron Natalie")
Date: Sun, 11 May 2003 21:48:28 +0000 (UTC) Raw View
""Espen Ruud Schultz"" <default@nospam.invalid> wrote in message news:AWqva.11302$8g5.175172@news2.e.nsc.no...
> Now why isn't this legal: ?
>
> int Test5i[ 5 ];
> Test5i = { 1, 2, 3, 4, 5 };
Two reasons:
1. There are no such things as array literals.
2. Arrays do not support assignment.
Yes it is braindamaged, but that's a long standing defect in C and C++.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "terjes."@chello.no (=?ISO-8859-1?Q?Terje_Sletteb=F8?=)
Date: Mon, 12 May 2003 18:21:11 +0000 (UTC) Raw View
ron@sensor.com ("Ron Natalie") wrote in message news:<L4zva.64066$3e3.21169@fe05.atl2.webusenet.com>...
> ""Espen Ruud Schultz"" <default@nospam.invalid> wrote in message news:AWqva.11302$8g5.175172@news2.e.nsc.no...
> > Now why isn't this legal: ?
> >
> > int Test5i[ 5 ];
> > Test5i = { 1, 2, 3, 4, 5 };
>
> Two reasons:
>
> 1. There are no such things as array literals.
> 2. Arrays do not support assignment.
>
> Yes it is braindamaged, but that's a long standing defect in C and C++.
There's a way to get a similar effect, using a class to encapsulate
the array, and overloading the assignment and comma operator,
something like:
#include <iostream>
template<class T,int Size>
class array
{
public:
array() : index(0) {}
array &operator=(const T &value)
{
index=0;
*this,value;
return *this;
}
array &operator,(const T &value)
{
array_[index]=value;
++index;
return *this;
}
T array_[Size];
int index;
};
int main()
{
array<int,10> test;
test=1,2,3;
for(int i=0;i!=test.index;++i)
std::cout << test.array_[i] << ' '; // Prints "1 2 3"
}
A technique like this is used in the Blitz++ library, to allow easy
assignment to matrices and vectors.
Of course, a language extension might instead support array assignment
and array literals directly.
Regards,
Terje
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]