Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!world!decwrl!infopiz!lupine!rfg From: rfg@NCD.COM (Ron Guilmette) Newsgroups: comp.lang.c++ Subject: Re: const is not object-oriented Message-ID: <2539@lupine.NCD.COM> Date: 11 Nov 90 18:07:20 GMT References: <1990Nov9.181408.23110@odin.corp.sgi.com> <60760@bbn.BBN.COM> Organization: Network Computing Devices, Inc., Mt. View, CA Lines: 48 In article <60760@bbn.BBN.COM> rgreen@spcpv3.bbn.com (Bob Green) writes: +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). People need to be aware that although the `casting aside constness' trick seems to work in most cases, and although it *is* >legal< it is not necessarily wise to rely on the assumption that the use of this trick will result in generated code which does what you expect on all current and future implementations of the language. If the ANSI C++ committee adopts language similar to that in the ANSI C standard regarding the semantics of `const' (and I see no reason why they should not), then the final ANSI C++ standard will say that the result of modifying a const (indirectly) via a pointer to a non-const are undefined. The implication is that some (very sophisticated) optimizers may one day cause any code which makes use of this `trick' to stop working. Be advised. -- // Ron Guilmette - C++ Entomologist // Internet: rfg@ncd.com uucp: ...uunet!lupine!rfg // Motto: If it sticks, force it. If it breaks, it needed replacing anyway.