C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Private const function

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Anna
Guest





PostPosted: Sat Jan 29, 2005 5:39 am    Post subject: Private const function Reply with quote



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





PostPosted: Sat Jan 29, 2005 9:33 am    Post subject: Re: Private const function Reply with quote



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





PostPosted: Sat Jan 29, 2005 9:34 am    Post subject: Re: Private const function Reply with quote



"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





PostPosted: Sat Jan 29, 2005 9:35 am    Post subject: Re: Private const function Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.