home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!elroy.jpl.nasa.gov!swrinde!mips!mips!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
- From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
- Newsgroups: comp.lang.c++
- Subject: Re: const on pass by value
- Message-ID: <1992Jul29.165553.28233@ucc.su.OZ.AU>
- Date: 29 Jul 92 16:55:53 GMT
- References: <1992Jul25.071559.4236@uunet.uu.net!mole-end> <9220801.5026@mulga.cs.mu.OZ.AU> <1992Jul27.154311.26719@cdsmn.mn.org>
- Sender: news@ucc.su.OZ.AU
- Organization: MAXTAL P/L C/- University Computing Centre, Sydney
- Lines: 59
- Nntp-Posting-Host: extro.ucc.su.oz.au
-
- In article <1992Jul27.154311.26719@cdsmn.mn.org> wells@cdsmn.UUCP (Rich Wells) writes:
- >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.
-
- No, it is the const in the DEFINITION that is important here.
-
- The definition
-
- void foo(int a) { ...}
-
- is clearly different to foo above.
-
- But the corresponding declarations
-
- foo(int);
- foo(const int);
-
- are ambiguous, they cannot be distinguished.
-
- --
- ;----------------------------------------------------------------------
- JOHN (MAX) SKALLER, maxtal@extro.ucc.su.oz.au
- Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
- ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
-