Confused about const
Paul D. Anderson
paul.d.removethis at comcast.andthis.net
Fri Mar 19 18:21:38 PDT 2010
bearophile Wrote:
> 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
My struct has a dynamic array as a member -- that seems to be the problem. This code doesn't compile:
struct S {
int x;
int[] a;
}
S foo(const S b) {
S other = b;
return other;
}
void main() {}
FYI, I'm using DMD version 2.30.
More information about the Digitalmars-d-learn
mailing list