const
Michel Fortin
michel.fortin at michelf.com
Fri Mar 28 05:50:07 PDT 2008
On 2008-03-28 04:26:42 -0400, "Janice Caron" <caron800 at googlemail.com> said:
> On 27/03/2008, Bill Baxter <dnewsgroup at billbaxter.com> wrote:
>> That sounds almost completely wrong to me. C++ programmers tend to use
>> 'const' in their function signatures to document the things that
>> shouldn't change. That sounds like self-documentation to me. What do
>> you mean by "rely on convention instead"? Please explain.
>
> class C
> {
> int ** p;
> }
>
> void f(const C &c)
> {
> **c.p = 1; // Legal in C++
> }
No, that's not legal in C++ because p is private (class members are
private in C++ unless explicitly made public). But beside that, is
legal.
> There is no way to express compiler-checked transitive constancy in C++.
There's a way if you really want it: keep the variable private and make
sure the accessor functions return a pointer corresponding to the
constancy you want, something like
const int * const * getP() const { return p; }
int ** getP() { return p; }
That's all a manual task to get there though (error prone?), and you
have to know about the type you're dealing with (which can be
complicated in templates).
--
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/
More information about the Digitalmars-d
mailing list