Re: const function() !!??
"Andrey Tarasevich" <andreytarasevi ch@hotmail.com> wrote in message
news:10tj3pqaud 7boc0@news.supe rnews.com...[color=blue]
> dandelion wrote:[color=green][color=darkred]
> >>...
> >> That's a good reason for returning a pointer-to-const-whatever, but I
> >> think the OP was asking why you would want to return a const-whatever
> >> (where "whatever" itself may or may not be a pointer type). As far as
> >> I can tell, there's no reason to do so.[/color]
> >
> > const int foobar[] =
> > {
> > FOOBAR1, /* Some config data */
> > FOOBAR2,
> > FOOBAR3
> > };
> >
> > const int get_foobar(int i)
> > {
> > assert(i<3);
> > return foobar[i];
> > }
> > ...[/color]
>
> And how and when is this better than just
>
> int get_foobar(int i)
>
> with the same body?[/color]
It's not much better anytime, anywhere. But hey... I was just looking for a
way (any way) that would make returning a const int a bit more sensible.
Best it could *possibly* do is to provide a hint to the programmer that
modifying the result of get_foobar(int i) may yield undesired results.
Otherwise the two are equivalent in all respects (as you know).
"Andrey Tarasevich" <andreytarasevi ch@hotmail.com> wrote in message
news:10tj3pqaud 7boc0@news.supe rnews.com...[color=blue]
> dandelion wrote:[color=green][color=darkred]
> >>...
> >> That's a good reason for returning a pointer-to-const-whatever, but I
> >> think the OP was asking why you would want to return a const-whatever
> >> (where "whatever" itself may or may not be a pointer type). As far as
> >> I can tell, there's no reason to do so.[/color]
> >
> > const int foobar[] =
> > {
> > FOOBAR1, /* Some config data */
> > FOOBAR2,
> > FOOBAR3
> > };
> >
> > const int get_foobar(int i)
> > {
> > assert(i<3);
> > return foobar[i];
> > }
> > ...[/color]
>
> And how and when is this better than just
>
> int get_foobar(int i)
>
> with the same body?[/color]
It's not much better anytime, anywhere. But hey... I was just looking for a
way (any way) that would make returning a const int a bit more sensible.
Best it could *possibly* do is to provide a hint to the programmer that
modifying the result of get_foobar(int i) may yield undesired results.
Otherwise the two are equivalent in all respects (as you know).
Comment