[540] in java-interest

home help back first fref pref prev next nref lref last post

const members and functions

daemon@ATHENA.MIT.EDU (Patrick Doane)
Thu Jun 29 11:14:39 1995

Date: Thu, 29 Jun 1995 10:58:30 -0400
To: java-interest@java.Eng.Sun.COM
From: pdoane@pcnet.com (Patrick Doane)

 I was having a discussion today with a co-worker about the meaning of
const on variables and functions. We discussed various aspects of it in c++
and I was interested if Java treated const any differently. The discussion
focused around an example presented in a recently new c++ book I was
browsing through:

 We start with a simple class, Fraction, that has one method reduce. In
c++, it could be declared like:

class Fraction {
    public int x;
    publit int y;

    void reduce() const;
};

 Since reduce doesn't change the external representation of the object, it
is considered a const function. However, it could quite easily change the
internal representation.

 There were a few ways around this:

1. Typecast the const away:

Fraction* access = (Fraction*)this;

2. The book I read talked about a proposed c++ keyword: mutable. This would
be applied to the integers x and y. Mutable variables could then be
modified by functions declared as const.


 Personally, I like the mutable idea but I feel that it removes an element
of safety internally to the class. I think it would make much more sense to
apply the mutable keyword to a function like this:

mutable void reduce();

 A mutable function would then be able to modify its internal data so long
as it didn't change its external appearance. This would allow for the code
to look clean and for the rest of the class to use const as normal.


 Has there been any discussion of the sort with Java, and if so what were
the results?

Patrick


-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post