On May 29, 2:50 pm, "Paul"<pchris...@yahoo.co.uk> wrote:
"?? Tiib"<oot...@hot.ee> 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.
Yes. Lot of smart people have felt that it is a problem and made
classes to wrap raw arrays so to get rid of it in C++. Usage of raw
arrays in real C++ production code is relatively low these days.
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
The 'p = arr' works, thanks to implicit arr to&arr[0] conversion. My
template above does not accept 'p' as int array argument anymore since
it is a true raw pointer unlike 'arr'. 'p' goes to cout as usual
void*. The 'x = arr' should indeed not to work because assigning whole
array to single int does not make sense.
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.
Yes. That is because C did not have references, so there were only two
ways to pass arrays in C (1) as pointer to first element (+ size as
separate argument) or (2) as member of struct. Both ways are indirect.
So the C designer's felt that writing '&arr[0]' everytime they needed
a pointer to first element of array too verbose syntax. C++ did not
change that because one of objectives of C++ is making migrating from
C to C++ as painless as possible and that is achieved by making most C
code to compile with C++ compiler.
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.
Yes, the pointer to what array implicitly converts (as it only can)
indirectly identifies the array. However array itself is located in
memory exactly where it's elements are, starting from first element.
Consider the code:
int arr[5]={0};
std::cout<< "&arr[0] = "<< &arr[0]<< std::endl;
std::cout<< "&arr = "<< &arr<< std::endl;
By language rules the address of first element of array and address of
array itself are same. So arr is object consisting of 5 ints and not a
separate pointer-like object (residing in memory elsewhere) that
points to the ints.