Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!dogie.macc.wisc.edu!uwvax!rutgers!dptg!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: const member functions ? Message-ID: <9633@alice.UUCP> Date: 18 Jul 89 12:53:21 GMT References: <6590205@hplsla.HP.COM> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 35 In article <6590205@hplsla.HP.COM>, jima@hplsla.HP.COM (Jim Adcock) writes: > Is there a way to declare a member function const -- IE to tell the compiler > that the member function does not change the value of this nor *this Yes [in 2.0]: class foo { int n; public: void setsize(int k) { n = k; } int getsize() const { return n; } }; If you say `const' in the declaration, and the definition isn't in the same place, you must also say `const' in the definition: class bar { int n; public: void setsize(int); int getsize() const; }; void bar::setsize(int k) { n = k; } void foo::getsize() const { return n; } In a member function defined with `const' this way, `this' is a pointer to a constant object. It is meaningless to say `const' for a constructor or destructor: they can operate on constants anyway. -- --Andrew Koenig ark@europa.att.com