Topic: Meaning of const in 'void f(int a) const' ?


Author: marwk@levels.unisa.edu.au
Date: 16 Jun 92 12:40:58 GMT
Raw View
Please tell me what 'const' means in the following:

class C
   {
   public:
       virtual void f(int a) const;
   }


Thank you
Raymond
--
University of South Australia   | Act in haste and repent at leasure
P.O. Box 1                      | Code too soon and debug forever
Ingle Farm                      | Knobs, knobs everywhere,
South Australia                 |              just vary a knob to think!




Author: kxh26@cas.org (Kenneth D. Huffman)
Date: Tue, 16 Jun 1992 20:00:21 GMT
Raw View
In article <17811.2a3e66f2@levels.unisa.edu.au> marwk@levels.unisa.edu.au writes:
>Please tell me what 'const' means in the following:
>
>class C
>   {
>   public:
>       virtual void f(int a) const;
>   }

It means the semi-colon cannot become a different punctuation symbol.  :-)

Actually, "const" after a member function prototype indicates the object will
not be modified by the member function. e.g.

 C c;
 int a;

 c.f(a);         // c will not be modified.

If "const C c;" were declared, only those member functions with this trailing
"const" could be called with "c."
--
"hello/ computer support/ my shift key doesn't seem to work." - e.e.cummings
Kenneth D Huffman   Work: Chemical Abstracts Service    Play: 2740 Wynnerock Ct
<kxh26@cas.org>           Box 3012, Columbus OH 43210         Hilliard OH 43026
<kxh26@cas.bitnet>        614-447-3838 ext. 2290              614-771-6334




Author: jimad@microsoft.com (Jim Adcock)
Date: 17 Jun 92 23:02:20 GMT
Raw View
In article <1992Jun16.200021.20462@cas.org> kxh26@cas.org (Kenneth D. Huffman) writes:
|Actually, "const" after a member function prototype indicates the object will
|not be modified by the member function. e.g.

Actually, "const" after a member function prototype indicates the object will
not be modified via the this pointer in the member function unless cast-from-
const is performed on the this pointer in which case the object could be
modified via this unless the object is of a class with no constructor
nor destructor and what does that mean given that the compiler will supply a
constructor for you if you don't specify one but in any case if the class
doesn't have a constructor nor destructor then whether or not the object will
be modified or won't be modified or something else happens is implementation
defined, and in addition the "const" after a member function differentiates
the member function from the otherwise identically declared "plain" member
function, "volatile" member function, and what about "const volatile" member
function, for the purposes of overloading resolution such that const member
functions are preferred on const objects and volatile member functions are
preferred on volatile objects and "plain" member functions are preferred on
"plain" objects except in cases where the overloading remains ambiguous in
which case it's an error but don't ask me to try to explain exactly when it
is or isn't ambiguous because I'm not sure anyone could explain even if it
did make any sense in the first place.