Connecting Tech Pros Worldwide Forums | Help | Site Map

const after function name

Robert
Guest
 
Posts: n/a
#1: Aug 18 '05
Hi all,

class Homer {
public:
int dot(int) const {return 1;}
...
}

what's the meaning of const after function name?

Best regards,
Robert


gevadas@gmail.com
Guest
 
Posts: n/a
#2: Aug 18 '05

re: const after function name


Const at the end of function tells the compiler that your function wont
modify any private variables of the class.You can only use these
functions with const objects.

Gevadas A. Akkara

Tim Love
Guest
 
Posts: n/a
#3: Aug 18 '05

re: const after function name


"Robert" <zhushenli@gmail.com> writes:
[color=blue]
>what's the meaning of const after function name?[/color]
Try
http://www-h.eng.cam.ac.uk/help/tpl/...+/argsC++.html

Dom Gilligan
Guest
 
Posts: n/a
#4: Aug 18 '05

re: const after function name


On 18 Aug 2005 01:29:33 -0700, "Robert" <zhushenli@gmail.com> wrote:
[color=blue]
>Hi all,
>
>class Homer {
>public:
> int dot(int) const {return 1;}
> ...
>}
>
>what's the meaning of const after function name?
>
>Best regards,
>Robert[/color]

'dot' doesn't modify anything in 'Homer'. You'll get warned if you do
try to modify something. Not necessary, but may enable optimisation.

Dom
Christian Meier
Guest
 
Posts: n/a
#5: Aug 18 '05

re: const after function name


<gevadas@gmail.com> schrieb im Newsbeitrag
news:1124354434.981058.279130@g47g2000cwa.googlegr oups.com...[color=blue]
> Const at the end of function tells the compiler that your function wont
> modify any private variables of the class.You can only use these
> functions with const objects.
>
> Gevadas A. Akkara
>[/color]

Not only private members.
const after function name makes the this-pointer const. So you are not
allowed to change a member variable in this function.

Greetings Chris


Earl Purple
Guest
 
Posts: n/a
#6: Aug 18 '05

re: const after function name


You won't "warned" you'll get a compiler error.
A const function is also not allowed to:
- Call a non-const member function
- Return by reference any member variable (other than by
const-reference).

A const function may return a member pointer (to non-const).

Frank Chang
Guest
 
Posts: n/a
#7: Aug 18 '05

re: const after function name


How about mutable member variables, can they be modified in const
member functions?

Karl Heinz Buchegger
Guest
 
Posts: n/a
#8: Aug 18 '05

re: const after function name


Frank Chang wrote:[color=blue]
>
> How about mutable member variables, can they be modified in const
> member functions?[/color]

Yes.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Jaspreet
Guest
 
Posts: n/a
#9: Aug 18 '05

re: const after function name



Frank Chang wrote:[color=blue]
> How about mutable member variables, can they be modified in const
> member functions?[/color]

Yes they are the 'only' kind of data members that can be modified in a
const member function.

Jack Klein
Guest
 
Posts: n/a
#10: Aug 19 '05

re: const after function name


On Thu, 18 Aug 2005 10:51:34 +0200, "Christian Meier" <chris@gmx.ch>
wrote in comp.lang.c++:
[color=blue]
> <gevadas@gmail.com> schrieb im Newsbeitrag
> news:1124354434.981058.279130@g47g2000cwa.googlegr oups.com...[color=green]
> > Const at the end of function tells the compiler that your function wont
> > modify any private variables of the class.You can only use these
> > functions with const objects.
> >
> > Gevadas A. Akkara
> >[/color]
>
> Not only private members.
> const after function name makes the this-pointer const. So you are not
> allowed to change a member variable in this function.[/color]

I hope you mean:

The const keyword after a member function name makes the object *this
constant inside the function.

'this' itself is always constant.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Closed Thread


Similar C / C++ bytes