Re: Problem with array objects
"?? Tiib" <ootiib@hot.ee> wrote in message
news:29661466-b6ec-4778-a77e-b00d00fa84f2@w36g2000vbi.googlegroups.com...
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
contiguous
integer objects are different entites is bases on the fact that they
contain
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?
The problem is not just ostream operator << , its every operator in the C++
language.
The identifier 'arr' is evaluated as an addresss in almost all situations
except typeid and sizeof:
int* p= arr; //This works
int x = arr; //This does not work
Also consider this :
template <typename T>
void foo(T t){
std::cout<< typeid(t).name();
}
foo(arr);
Given a free choice arr is passed as a pointer and not a reference.
The identifier 'arr' does not behave like identifiers to other objects in
C++ it is somewhat special.
I can agree that it is a non modifiable array-type object. But it does not
identify an array in the same sense that other objects' names identify them,
it indirectly identifies the array, because it must be dereferenced to
access the array of objects that it identifies.