const(FAQ)

Janice Caron caron800 at googlemail.com
Tue Apr 1 07:50:55 PDT 2008


On 01/04/2008, Jason House <jason.james.house at gmail.com> wrote:
> This is actually a good way to think about this stuff.  My current problem with the const regime is that global variables can be used to break the transitive const nature.

I think you are incorrect. In a class such as the following

    int g;

    class C
    {
        int * p;

        this()
        {
            p = &g;
        }
    }

Then g cannot be modified through a const(C).

    const C c = new C;
    *c.g = 4; /*ERROR*/

What you're talking about is something different. You're talking about
a member function which is declared const accessing global variables.
(Or at least, so I assume). Well, that's not a violation of anything.
A /const/ function gets a read-only view of "this", but can still read
and write global variables. A /pure/ function cannot see global
variables at all.


>  I really hate to see designs that encourage use of global variables!

Every (non-pure) function ever can have some paramters const and
others not. For member functions, one of those parameters happens to
be "this", but that has no bearing on whether or not global variables
are seen as const.

Nothing "encourages" you to use global variables. However, rest
assured that in the future, it will be possible to declare functions
"pure", which will render global variables completely inaccessible.



More information about the Digitalmars-d mailing list