Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uwm.edu!wuarchive!zaphod.mps.ohio-state.edu!usc!jarthur!uunet!tdatirv!sarima From: sarima@tdatirv.UUCP (Stanley Friesen) Newsgroups: comp.lang.c++ Subject: Re: const Message-ID: <137@tdatirv.UUCP> Date: 29 Aug 90 15:22:45 GMT References: <5027@atexnet.UUCP> Reply-To: sarima@tdatirv.UUCP (Stanley Friesen) Organization: Teradata Corp., Irvine Lines: 35 [I tried to mail this, but it bounced, so here it is] In article <5027@atexnet.UUCP> you write: >I have a question about the use of const in member functions. It seems to me taht there are three different uses for const in member functions. They are: > >1) constant arguments to the function A::f(const x) >2) constant return type from the function const char* A::g() >3) constant function where the object or 'this' is a constant. > >The second and third types seems to confuse me. Are there really two distinct >types, and what is the syntax. Yes, and in fact #3 is more closely related to #1 than #2 (since the object is passed as hidden extra argument in most implementations). Case #2 is used where the function returns some private data that should not be modified by the caller. Case #3 is used where the member function should not be allowed to modify the state of the object it is invoked on. > >I have seen > >int A::f() const; in some code as well as int const A::f(); and I am not sure >if there is a diference. The former syntax does not seem to be accepted by g++. You seem to have an old version of g++. The former syntax is new to cfront version 2.0, and its equivalents from other vendors. (The latest g++ does in fact accept it, I believe). int A::f() const; declares a 'constant function' where 'this' is constant. int const A::f(); is the same as: const int A::f(); and declares a function returning a constant result, which is only meaningful if the function returns a pointer or a reference.