Re: Problem with array objects
On May 28, 5:11 am, "Paul" <pchris...@yahoo.co.uk> wrote:
"Joshua Maurice" <joshuamaur...@gmail.com> wrote in message
--His reason for believing so is one part of the C++ standard which
--mentions that array objects are not modifiable.
No you are incorrect. My opinion that the object 'arr' and the 5 contiguo=
us
integer objects are different entites is bases on the fact that they cont=
ain
contain different values.
in the following code:
int arr[5]={0};
std::cout<< arr;
std::cout<< arr[0];
The value accessed by arr is not any value that is stored within the 5
contiguous integer objects.
If 'arr' was truly the same as the 5 integer objects then it would store =
the
same value.
Of course, arr stores all of them 5 integer objects. It is array after
all. Your example does not show it since ostream is not made to
display whole arrays. ostream can display a void* and that is into
what any array converts to as soon someone coughs at it (bloody legacy
from C). But we can help ostream out easily to accept int arrays too.
See:
#include<iostream>
/// Helping poor ostream to output int arrays
template<int N>
std::ostream& operator<<( std::ostream& o, int const (& a)[N] )
{
o << "{";
for ( int i = 0; i < N; ++i )
{
o << a[i];
if ( i < N-1 ) o << ",";
}
o << "}";
return o;
}
int main()
{
// Pauls code
int arr[5]={0};
std::cout << "arr =" << arr << std::endl;
std::cout << "arr[0] =" << arr[0] << std::endl;
// that is what arr converted to without our operator
std::cout << "&arr[0] = " << &arr[0] << std::endl;
}
The output is:
arr = {0,0,0,0,0}
arr[0] = 0
&arr[0] = 0012FF50
arr is still array of 5 ints. Can't be Paul you are arguing even
against that?