home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!microsoft!hexnut!jimad
- From: jimad@microsoft.com (Jim Adcock)
- Subject: Re: const on pass by value
- Message-ID: <1992Jul27.185931.5433@microsoft.com>
- Date: 27 Jul 92 18:59:31 GMT
- Organization: Microsoft Corporation
- References: <1992Jul24.151010.11969@PacBell.COM>
- Lines: 40
-
- In article <1992Jul24.151010.11969@PacBell.COM> pjcondi@lepton (Paul Condie) writes:
- |Should pass by value arguments be declared as "const" for
- |good coding style? Or is that nit picking?
- |
- |For example:
- |
- |void foo (const int a);
- |or
- |void foo (int a);
-
- The confusion here is that declaring a param really declares two different
- things:
-
- 1) It declares the external interface to the function
-
- 2) It declares the internal type of the param.
-
- In both cases above the external interface is to a function foo taking an
- int returning void.
-
- The first case makes the further claim that the parameter will be used
- as a constant within the function. This claim about the internal use
- of "a" might be very useful to the implementor of the function, the
- maintainer of the function, and in some cases might even aid the
- code generator in creating better machine code.
-
- So, yes, declaring the param "const" is a "good idea" even if the constness
- only applies to the "internal" use of the param within the function.
-
- Note that this param declaration is a very different situation from
- the similar looking:
-
- void foo(const Bar& b)
-
- where the "const" represents BOTH an external interface and a internal
- constraint on the use of b [note that people from the Humpty-Dumpty camp would
- claim otherwise that the "const" only refers to the *external* interface, and
- that *internal* to foo the function implementor should be free to molest
- b as they so desire. Currently C++ permits such molestation, but I would
- claim that people doing so are showing very bad programming style]
-