← Back to team overview

dolfin team mailing list archive

Re: const in la classes

 



Anders Logg wrote:
On Thu, Mar 15, 2007 at 10:12:09PM +0100, Garth N. Wells wrote:

What's the problem with const? Not much might happen in these functions, but M and N are not meant to be changed by the init function, so we might as well use const.

Garth

Yes, but one could say this is the business of the body/implementation
of the function. M and N (or other const variables) are passed by
value so to the caller they will always remain constant. The function
does not need to announce to the outside world what it does
internally.


True, but I see const and protecting the init function from itself (or more accurately, the person writing init()). If const can be used, why not use it?

If we have

    bar(const Foo& foo);

then the const means something (we could change the value of foo
inside the function were it not for the const), but to the outside
world, there is no difference between these two:

    bar(foo)
    bar(const foo);

If we want to have the const on everything passed by value, then we
should also have

    const int* const rows

as well in the argument list, but

    const int* rows

should be enough (we promise not to change the values in the array
rows), but we might (internally in the body of the function) change the
value of the local copy of the pointer (the pointer is passed by
value).


But is this a reason for not using const in init()?

Garth

/Anders





Follow ups

References