Arrays passed by reference by default?

前端 未结 3 752
死守一世寂寞
死守一世寂寞 2020-12-14 01:51

I\'m reading a C++ book which says this:

C++ passes arrays to functions by reference—the called functions can modify the element values in the callers

3条回答
  •  眼角桃花
    2020-12-14 02:00

    You're right. The wording is very confusing and uses a meaning of "reference" that is not the same as the term reference relating to the C++ feature of the same name. Instead, it's talking about the way that array names decay to pointers — in fact you do not "pass an array" like this at all!

    In the "olden days" "reference" was used in a more general sense in the same way as "handle" — an abstract term to represent the use of indirection to fake by-reference semantics in languages that did not support it. But, C++ does support things that it calls references; thus, we tend not to use "reference" in its "handle" sense when talking about C++ (where Deitel ∉ "we", evidently).

    Recommended reading:

    • http://jcatki.no-ip.org/fncpp/Resources
    • The Definitive C++ Book Guide and List

    Any other C++ book be very wary!! Though in the majority of areas of life it would be insane of me to suggest that inclusion in the above two specific lists is a definitive pre-requisite for a book to be considered "good", there is a sufficient wealth of dangerously incorrect C++ text out there (such as the text you quote) and this is a sufficiently big problem for our language newcomers that in the world of C++ books it's actually a good rule of thumb to follow.

提交回复
热议问题