JKop said:
Andrey Tarasevich posted:
Method Man wrote:
...
If I create a type with an array as a data member, and I'm allowed to
pass an object of my type by value into a function, wouldn't all its
members be copied over (incl. the array)?
Yes.
So how would this work?
Hmm... They are just copied. One subobject after another. Which also
applies to arrays. Arrays are copied element by element.
Even if you have complicated ones:
struct blah
{
std::string a[6];
std::vector b[8];
};
When you pass it by value, their copy constructors will be
called, ie. 6 copy constructed strings, 8 copy constructed
vectors!
Ok, I think I understand now. I'll attempt to summarize what I got from this
thread.
An array itself can't be passed by value because it is not an object and not
copy-constructable. But if we simply wrap our array in a struct or class and
pass an object of that type by value, the compiler copies the array for us
element-by-element.
No, an array is an object. The definition of the term 'object' in C++
has nothing at all to do with 'object oriented programming' or with
classes or user defined types.
Bare arrays are not copyable in C and C++ because of limitations that
existed when C was developed 30 odd years ago, given the type of
programming and programmers it was originally intended for.
If either C or C++ was ever changed to make bare arrays copyable, or
passable to and returnable from functions, some new syntax would have
to be developed, it couldn't be just by using the unadorned name of
the array.
If the automatic conversion from array name to pointer to first
element in function calls and return statements was ever changed, it
would break 99% of all the C and C++ programs that have ever existed.
Sounds to me like the original restriction on arrays is pointless. I guess
I'll live
It wouldn't have been so pointless if, 30 plus years ago, you were an
expert systems programmer on the typical small minicomputers of the
day and your objective was to make a reasonably portable high level
language compact and efficient enough so that it could be used to
write operating systems, instead of assembly language in which they
were all written in up to that time.
I doubt if anyone was more surprised than Dennis Ritchie to see the
range of platforms and applications that C has taken on. Not to
mention the even larger applications done in C++.