Discussion:
const after function name
(too old to reply)
Robert
2005-08-18 08:29:33 UTC
Permalink
Hi all,

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

what's the meaning of const after function name?

Best regards,
Robert
Tim Love
2005-08-18 08:39:09 UTC
Permalink
Post by Robert
what's the meaning of const after function name?
Try
http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/argsC++.html
g***@gmail.com
2005-08-18 08:40:35 UTC
Permalink
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
Christian Meier
2005-08-18 08:51:34 UTC
Permalink
Post by g***@gmail.com
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
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
Jack Klein
2005-08-19 02:25:19 UTC
Permalink
Post by Christian Meier
Post by g***@gmail.com
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
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.
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/~ajo/docs/FAQ-acllc.html
Dom Gilligan
2005-08-18 08:48:25 UTC
Permalink
Post by Robert
Hi all,
class Homer {
int dot(int) const {return 1;}
...
}
what's the meaning of const after function name?
Best regards,
Robert
'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
Earl Purple
2005-08-18 11:06:19 UTC
Permalink
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
2005-08-18 14:39:02 UTC
Permalink
How about mutable member variables, can they be modified in const
member functions?
Karl Heinz Buchegger
2005-08-18 15:10:13 UTC
Permalink
Post by Frank Chang
How about mutable member variables, can they be modified in const
member functions?
Yes.
--
Karl Heinz Buchegger
***@gascad.at
Jaspreet
2005-08-18 16:25:00 UTC
Permalink
Post by Frank Chang
How about mutable member variables, can they be modified in const
member functions?
Yes they are the 'only' kind of data members that can be modified in a
const member function.
Continue reading on narkive:
Loading...