any news on const/invariant?
Walter Bright
newshound1 at digitalmars.com
Wed Nov 28 17:43:25 PST 2007
Bill Baxter wrote:
> Here's a class that compiles fine and will probably mostly work.
>
> class Klass {
>
> this() {
> array.length = 10;
> }
> const int elem(int i) {
> return array[i];
> }
>
> int[] array;
>
> }
>
> But someday when someone tries to call elem() with a const Klass it will
> fail to compile.
Yes. So where's the problem? Also, why would one want to return a "const
int"? It doesn't make much sense.
> Also in case anyone else wasn't sure, this is how you declare the common
> idiom of const method returning a const reference in the latest D2.008:
>
> const const(int*) get_elem_ptr(int i) {...}
>
> Without the parens it gives "Redundant storage class". (However
> declaring it "public public public" still does not create an error...)
>
> Also const int* get_elem_ptr(int i) fails to compile because it tries to
> return a mutable pointer from a const method. So that case is ok, but
> the one in Klass above seems like trouble brewing to me.
>
> --bb
More information about the Digitalmars-d
mailing list