home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!dtix!darwin.sura.net!mips!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!ames!agate!forney.berkeley.edu!jbuck
- From: jbuck@forney.berkeley.edu (Joe Buck)
- Newsgroups: comp.lang.c++
- Subject: Re: HELP: mixing const and non const function
- Message-ID: <14i99nINN2s7@agate.berkeley.edu>
- Date: 22 Jul 92 00:14:47 GMT
- References: <1992Jul21.182115.1@cuavax.dnet.cua.edu>
- Organization: U. C. Berkeley
- Lines: 33
- NNTP-Posting-Host: forney.berkeley.edu
-
- In article <1992Jul21.182115.1@cuavax.dnet.cua.edu> 48ganelin@cuavax.dnet.cua.edu writes:
- > I want to declare a function to be constant though it
- > uses a nonconstant function.
- > (Of course it is an error for usual situation, but see example)
-
- The example code is
- class foo {
- int vector [10] ;
- public:
- int& operator () (int at) { return vector[at] ; }
- };
-
- class foo2
- {
- foo f ;
- int median (void) const { return f(5); } // const violation
- };
-
- You can get the effect you want by changing foo to
-
- class foo {
- int vector [10] ;
- public:
- int& operator () (int at) { return vector[at] ; }
- const int& operator () (int at) const { return vector[at] ;}
- };
-
- foo2 will now use the const version in its median function.
-
-
-
- --
- Joe Buck jbuck@ohm.berkeley.edu
-