Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!gem.mps.ohio-state.edu!tut.cis.ohio-state.edu!ucbvax!hplabs!hp-pcd!hplsla!jima From: jima@hplsla.HP.COM (Jim Adcock) Newsgroups: comp.lang.c++ Subject: Re: the use of const Message-ID: <6590263@hplsla.HP.COM> Date: 26 Sep 89 18:22:20 GMT References: <740@mit-amt.MEDIA.MIT.EDU> Organization: HP Lake Stevens, WA Lines: 20 > Is it desirable from an efficiency point of view to use the const modifier > wherever applicable? > > I find myself hardly using it, although many (most?) of my functions do not > change any of their arguments. I am wondering whether the compiler (cfront) > will actually generate "better" code if I give it these hints. > > I understand the implications of its use as far as protecting myself, but > mostly functions are so small that I don't usually screw myself in this > way (type checking). On the other hand it is a nice hint to see a > declaration such as form(const char* ...) which tells me that a certain > argument will not be changed by the function.... Const is important, and you need to learn to use it. When first exposed to 2.0 compatible compilers, const seems real strange, but after a while it starts to become natural. Without declaring functions "const" you force the compiler to introduce many "unnecessary" temporary variables. At least on AT&T's compiler you can see this by turning on the warning option. The const'ness of an object or a function is a very important attribute.