Topic: Question for Static member function


Author: Philippe Nobili <pnobili@imaginet.fr>
Date: 1998/04/06
Raw View
Jerry Coffin wrote:
>
> In article <3511A18E.66CAF8FC@qualcomm.com>, tzhang@qualcomm.com
> says...
> > Hi there,
> >
> > Maybe this sounds silly - I found static class member functions are not
> > allowed to be const.  Can somebody tell me why?  From my point of view,
> > it makes sense to me that a static "get-xxx" member function is declared
> > as const.
>
> Declaring a member function const means that it won't modify any non-
> mutable variables that are members of the object on which it is
> invoked.
>
> Since a static member function is NOT invoked on an object (since it's
> associated with the class as a whole, rather than a specific object of
> the class) and doesn't receive a "this" when it's invoked, the basic
> concept of being const simply doesn't apply.
>
> IOW, when you invoke a normal member function, it receives a pointer
> to an object.  In the case of a const member function, it receives a
> pointer to a const object.  However, since a static member function
> doesn't receive ANY pointer to an object, it doesn't make any real
> sense to say that the pointer it's not receiving is const or
> otherwise.

Yes, but it could make sense to extend the use of 'const' to static
members functions, where it could be used to tell that static member
data is not modified by the function ...

The only thing that does not really make any sense is the use of
'static' where 'class' is intended. It makes sense to have class member
functions that do not affect class member data, IMO.

Philippe.

--Ambush_of_Tigers_016_000--
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1998/04/02
Raw View
[Moderator's note: apologies for the long delay in approving this article.
 -moderator of comp.std.c++ (fjh).]

In article <3511A18E.66CAF8FC@qualcomm.com>, tzhang@qualcomm.com
says...
> Hi there,
>
> Maybe this sounds silly - I found static class member functions are not
> allowed to be const.  Can somebody tell me why?  From my point of view,
> it makes sense to me that a static "get-xxx" member function is declared
> as const.

Declaring a member function const means that it won't modify any non-
mutable variables that are members of the object on which it is
invoked.

Since a static member function is NOT invoked on an object (since it's
associated with the class as a whole, rather than a specific object of
the class) and doesn't receive a "this" when it's invoked, the basic
concept of being const simply doesn't apply.

IOW, when you invoke a normal member function, it receives a pointer
to an object.  In the case of a const member function, it receives a
pointer to a const object.  However, since a static member function
doesn't receive ANY pointer to an object, it doesn't make any real
sense to say that the pointer it's not receiving is const or
otherwise.

--
    Later,
    Jerry.

The Universe is a figment of its own imagination.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Tony Zhang <tzhang@qualcomm.com>
Date: 1998/03/19
Raw View
Hi there,

Maybe this sounds silly - I found static class member functions are not
allowed to be const.  Can somebody tell me why?  From my point of view,
it makes sense to me that a static "get-xxx" member function is declared
as const.

Thank you for your attention,

Tony



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Piet Van Vlierberghe" <pieter.vanvlierberghe@NOSPAM.lms.be>
Date: 1998/03/24
Raw View
Tony Zhang <tzhang@qualcomm.com> wrote in article
<3511A18E.66CAF8FC@qualcomm.com>...
> Hi there,
>
> Maybe this sounds silly - I found static class member functions are not
> allowed to be const.  Can somebody tell me why?  From my point of view,
> it makes sense to me that a static "get-xxx" member function is declared
> as const.
>
A const member function of class X is a function in which 'this' is typed
as pointer to const:
const X *this
As opposed to the usual type:
X *this

A static member function is a member function in which 'this' is not
accessible, so the concept of
a const member function and the concept of a static member function are
mutually exclusive.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]