home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!The-Star.honeywell.com!umn.edu!mmm.serc.3m.com!pwcs!cdsmn!wells
- From: wells@cdsmn.mn.org (Rich Wells)
- Subject: Re: const on pass by value
- Message-ID: <1992Jul27.154311.26719@cdsmn.mn.org>
- Reply-To: wells@cdsmn.UUCP (Rich Wells)
- Organization: Dicomed, Inc., Burnsville, MN
- References: <1992Jul24.151010.11969@PacBell.COM> <1992Jul25.071559.4236@uunet.uu.net!mole-end> <9220801.5026@mulga.cs.mu.OZ.AU>
- Date: Mon, 27 Jul 92 15:43:11 GMT
- Lines: 42
-
- In article <9220801.5026@mulga.cs.mu.OZ.AU> fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
- >mat@uunet.uu.net!mole-end writes:
- >
- >>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);
- >>
- >>It's not nitpicking. It's part of something called const correctness.
- >>Your program has it or doesn't. Programs that have it are almost immune
- >>to a certain large family of errors.
- >
- >Huh? In this example, it IS just nitpicking.
- >How could the presence or absence of const in the declaration for foo
- >affect the program's const correctness?
- >
-
- Consider this case:
-
- void bar(int & a)
- {
- }
-
- void foo(const int a)
- {
- bar(a);
- }
-
- Should the compiler allow the call to bar() from foo()? I think not.
- I'm still a relative newcomer to C++ but it seems to my this is an
- example of where the presence or absence of const in the declaration
- of foo DOES affect the program's const correctness.
-
- Granted, this is a contrived example, but replace the 'int' with
- a 'struct' and it is no longer quite so contrived.
-
- Richard Wells wells@cdsmn.mn.org or ...!tcnet!cdsmn!wells
-