Confused about const

bearophile bearophileHUGS at lycos.com
Fri Mar 19 18:10:22 PDT 2010


Paul D. Anderson:

> 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.

This program compiles:

struct S { int x; }
S foo(const S a) {
    S other = a;
    return other;
}
void main() {}


So, what's that you actually write? When possible show programs that run! It's one of the things I have learnt from the Python groups.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list