From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!elroy.jpl.nasa.gov!zardoz.cpd.com!dhw68k!felix!ka@felix.UUCP From: ka@felix.UUCP (Kenneth Almquist) Newsgroups: comp.lang.ada Subject: Re: Reference vs. copy semantics in passing parameters Message-ID: <163815@felix.UUCP> Date: 28 May 91 05:28:29 GMT References: <2725@sparko.gwu.edu> <2742@sparko.gwu.edu> <1991May16.135103.1688@software.org> Sender: daemon@felix.UUCP Organization: FileNet Corp., Costa Mesa, CA List-Id: >In article <2742@sparko.gwu.edu> mfeldman@seas.gwu.edu () writes: >> In C++, you can declare not only the pointer constant but the pointed >> to construct constant as well. This allows passing by reference in a >> read-only manner, which is NOT possible in Ada at present. This feature has also been included in the ANSI C standard. blakemor@software.org (Alex Blakemore) asks: > This sounds like a nice safety feature but can callers really rely on it ? > Even if the C++ language prevents updating the object if the pointer is > declared appropriately, does it prevent assignment to a normal pointer > which will allow the referenced object to be updated ? The "constant" attribute is part of the type, so the type checking system makes this secure *unless* the type system is circumvented by the use of a type cast. (By "type cast", I mean the C equivalent of Ada's UNCHECKED_CONVERSION.) The rule is that a pointer to a non-constant object can be assigned to a pointer to a constant object, but not the other way around. (Assignment includes parameter passing.) A problem with the "constant" attribute is that it is not inherited properly. Consider a routine that looks up an entry in a data structure like a tree or a string, and returns a pointer to the element found. The returned pointer should point to a constant object iff the data structure was declared to be constant, but the C type system doesn't handle this. For this reason, several routines in the standard ANSI C library are defined in such a way that they cannot be implemented without using type casts. Kenneth Almquist