Xref: utzoo comp.lang.c++:10293 comp.std.c++:405 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!usc!apple!bbn.com!rgreen From: rgreen@bbn.com (Robert W. Green) Newsgroups: comp.lang.c++,comp.std.c++ Subject: Re: const is not object-oriented Message-ID: <60760@bbn.BBN.COM> Date: 10 Nov 90 00:33:54 GMT References: <1990Nov9.181408.23110@odin.corp.sgi.com> Sender: news@bbn.com Reply-To: rgreen@spcpv3.bbn.com (Bob Green) Followup-To: comp.lang.c++ Organization: Bolt Beranek and Newman Inc., Cambridge MA Lines: 38 In article <1990Nov9.181408.23110@odin.corp.sgi.com> linton@sgi.com (Mark Linton) writes: >The "const" keyword in C++ specifies a property of storage, and >as such is not particularly useful in an object-oriented program. >Declaring a parameter or a member function as const requires >knowledge about the implementation of a class. > ... example deleted >The bottom line is if you are defining a C++ class, especially >for a library, do not use const parameters or const member functions. >Const means storage, not behavior, so it is by definition >an implementation/representation concept. Whoa, I think you are throwing out the baby with the bath water. The ability to declare methods as const is just too valuable for the design of large systems to just throw out. The language specification may need some tuning but I wouldn't just toss it. Consider your example. It can be implemented using a cast of the form: ((IntVector *const) this)->current_sum = /* sum calculation */ This works with g++. I haven't tried it with cfront but I think it should be legal c++ code (I lifted the type spec. from page 177 of E&S which defines the declaration of this). Now I would agree that use of a cast is fairly ugly but I have difficulty thinking of a mechanism which doesn't paper over a similiar hole in the type system. As ugly as it is, an occasional cast is a big improvement over dropping use of const everywhere. -Bob PS. I have implemented a class which does exactly the caching of statistical calculations in your example. However, in my implementation, the cached statistics are maintained as a side effect of the update methods defined for the data vector. Since changing the data vector is by definition not a const member function, I was able to update the cached statistics without violating the type system or using a ugly cast. Again, dropping use of const seems to be overkill.