|
|
Up |
|
|
  |
Author: subramanian100insubramanian100in Date: Nov 12, 2007 23:43
why can't a static member function be declared as const ?
We can declare a non-static member function as const,
to indicate that it does not modify the non-mutable data
members of a class. In the same way, is there a provision
to indicate that a static member function does not modify
the static data members but only uses their values ?
Kindly explain.
Thanks
V.Subramanian
|
| |
|
| | 4 Comments |
|
  |
Author: Daniel KrüglerDaniel Krügler Date: Nov 13, 2007 08:29
On Nov 13, 8:43 am, "subramanian10...@ yahoo.com, India"
yahoo.com> wrote:
> why can't a static member function be declared as const ?
> We can declare a non-static member function as const,
> to indicate that it does not modify the non-mutable data
> members of a class.
Slightly imprecise: The const-qualification specifies whether
the member function does potentially modify a given instance
of a class, more specifically a const member function can be
thought of having a hidden this-pointer to the *const* class
type and acting with this pointer. From this we can easily
recognize that mutable operations conflict with this const-
qualification. Static member functions don't have any this
pointer.
> In the same way, is there a provision
> to indicate that a static member function does not modify
> the static data members but only uses their values ?
|
| Show full article (1.71Kb) |
|
| | no comments |
|
  |
Author: WaterWalkWaterWalk Date: Nov 13, 2007 08:30
On Nov 13, 3:43 pm, "subramanian10...@ yahoo.com, India"
yahoo.com> wrote:
> why can't a static member function be declared as const ?
> We can declare a non-static member function as const,
> to indicate that it does not modify the non-mutable data
> members of a class. In the same way, is there a provision
> to indicate that a static member function does not modify
> the static data members but only uses their values ?
>
I think one reason to use const member function is that if the
function is called via a const class object, then there shall be some
way to inform the compiler that "this function does not modify the
object". But a static member function call don't need a object of that
class. So I don't think there is a need for const static member
function.
If you don't want a static member function modify a static member,
just take some care not to modify. Or you may use a const reference to
that static data member inside you static member function.
|
| Show full article (1.29Kb) |
| no comments |
|
  |
Author: Roman.PerepelitsaRoman.Perepelitsa Date: Nov 14, 2007 11:02
On 13 Nov, 10:43, "subramanian10...@ yahoo.com, India"
yahoo.com> wrote:
> why can't a static member function be declared as const ?
> We can declare a non-static member function as const,
> to indicate that it does not modify the non-mutable data
> members of a class. In the same way, is there a provision
> to indicate that a static member function does not modify
> the static data members but only uses their values ?
>
> Kindly explain.
It would be pointless, because you can easily remove
const without a cast.
struct foo
{
static void bar() const;
int i;
};
int & get_foo_i() { return foo::i; }
|
| Show full article (1.04Kb) |
| no comments |
|
  |
|
|
  |
Author: Roman.PerepelitsaRoman.Perepelitsa Date: Nov 15, 2007 09:06
Daniel Krügler wrote:
> On 14 Nov., 20:02, "Roman.Perepeli...@ gmail.com"
> gmail.com> wrote:
>> It would be pointless, because you can easily remove
>> const without a cast.
>>
>> struct foo
>> {
>> static void bar() const;
>
> This is not allowed, but let it remain for a while, because
> you are obviously in the process to explain something.
>
>> int i;
>>
>> };
>>
>> int & get_foo_i() { return foo::i; }
>
> This should not compile, but what was your intend? ...
|
| Show full article (1.72Kb) |
| no comments |
|
RELATED THREADS |
  |
|
|
|