Confused about const

Paul D. Anderson paul.d.removethis at
Fri Mar 19 17:58:21 PDT 2010

I created a struct, call it "S", and some functions that operate on S. But I'm confused about when const is useful.

Being an old Java programmer, I use 'const' the same as I used 'final' in Java. So most of my functions look like this:

S for(const S a, const S b) {
    S x = a;
    S y = b;
    // do some stuff
    return a;

I declare the parameters const and then copy them to work on them.

I get error messages about not implicitly casting const S to S. So I can make an explicit cast:

    S x = cast(S) a;
    S y = cast(S) b;

and the error messages go away.

But I think I must have gone off the rails somewhere -- all I want is for the caller to be sure the function doesn't alter a and b. But I end up with lots of casts to/from const. What am I missing??


More information about the Digitalmars-d-learn mailing list