| View previous topic :: View next topic |
| Author |
Message |
Anna Guest
|
Posted: Sat Jan 29, 2005 5:39 am Post subject: Private const function |
|
|
What is one good reason to declare a private function const (other than
the reason that it doesn't modify any class member)?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
adeht Guest
|
Posted: Sat Jan 29, 2005 9:33 am Post subject: Re: Private const function |
|
|
Anna wrote:
| Quote: | What is one good reason to declare a private function const (other than
the reason that it doesn't modify any class member)?
|
Perhaps the fact that a public/protected member function that is
declared const won't be able to call a member function (for 'this') that
is declared non-const.
It also makes it easier to understand code.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ivan Vecerina Guest
|
Posted: Sat Jan 29, 2005 9:34 am Post subject: Re: Private const function |
|
|
"Anna" <swetha.karthik (AT) gmail (DOT) com> wrote
| Quote: | What is one good reason to declare a private function const (other than
the reason that it doesn't modify any class member)?
|
Allowing it to be called from other (public) const member functions.
Consider:
class C
{
private:
int* getInternalPointer() const; // the private const member
public:
int get3rdValue() const { return getInternalPointer[2]; }
};
If the private member were not const, get3rdValue() would not compile.
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Sat Jan 29, 2005 9:35 am Post subject: Re: Private const function |
|
|
Anna wrote:
| Quote: | What is one good reason to declare a private function const (other than
the reason that it doesn't modify any class member)?
|
So that you can call it from another const member function, which could
be public.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|