Topic: static method
Author: grumpy@cbnewse.cb.att.com (Paul J Lucas)
Date: Sat, 14 Aug 1993 17:01:01 GMT Raw View
Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Sun, 15 Aug 1993 21:31:02 GMT Raw View
In article <1993Aug13.194652.2637@rcmcon.com> rmartin@rcmcon.com (Robert Martin) writes:
>ldang@speech1.NoSubdomain.NoDomain (Student) writes:
>> class foo {
>> static int x;
>> static int y;
>> int z;
>> public:
>> static void f() const; // compiler complains
>> ....
>> };
>>In foo::f(), I don't change neither foo::x nor foo::y, so why
>>can't I declare foo::f() "const" ?
>
>static functions do not operate on any particular instance of a class. So
>they cannot be considered 'const'.
Could someone please cite chapter and verse (either from the ARM or from
recent X3J16 working papers) where the rule which Robert alludes to is
actually stated?
I'd be greatful. Thanks.
--
-- Ronald F. Guilmette ------------------------------------------------------
------ domain address: rfg@netcom.com ---------------------------------------
------ uucp address: ...!uunet!netcom.com!rfg -------------------------------
Author: rmartin@rcmcon.com (Robert Martin)
Date: Mon, 16 Aug 1993 19:51:56 GMT Raw View
rmartin@rcmcon.com (Bob Martin) writes:
>>static functions do not operate on any particular instance of a class. So
>>they cannot be considered 'const'.
rfg@netcom.com (Ronald F. Guilmette) writes:
>Could someone please cite chapter and verse (either from the ARM or from
>recent X3J16 working papers) where the rule which Robert alludes to is
>actually stated?
ARK spoke of theorems in one of his recent postings. I haven't
written a formal proof for quite awhile, so I thought I'd try to be
very erudite and give it a go. Here is a theorem which supports my
claim that a static member function cannot have the const qualifier.
1. Assume static member function can be const.
2. ARM 9.3.1 states "The type of 'this' in a member function of class
X is X *const unless the member function is declared 'const' [...]
in [that case], the type of 'this' is const X *const...
3. Thus, all const functions have a 'this'. (This may be a bit of a
stretch, however I think it is supportable by working backwards
from the subsequent statement which is: "A function declared const
and volatile has a 'this' with the type const volatile X *const.").
4. So a const static member function has a 'this'.
5. ARM 9.4 states: "A static member function does not have a 'this'
pointer.
6. Statement 5 contradicts statement 4. Statement 1. leads to a
contradiction, and so cannot be true. Therefore: Static member
functions cannot be const.
Well, I don't know how worthwhile that was, but it was fun.
--
Robert Martin | Design Consulting | Training courses offered:
Object Mentor Assoc.| rmartin@rcmcon.com | Object Oriented Analysis
2080 Cranbrook Rd. | Tel: (708) 918-1004 | Object Oriented Design
Green Oaks IL 60048 | Fax: (708) 918-1023 | C++
Author: ldang@speech1.NoSubdomain.NoDomain (Student)
Date: Thu, 12 Aug 93 17:58:28 GMT Raw View
Hi experts !
Let's have the following class
class foo {
static int x;
static int y;
int z;
public:
static void f() const; // compiler complains
....
};
My question is :
In foo::f(), I don't change neither foo::x nor foo::y, so why
can't I declare foo::f() "const" ?
Conceptually, I should be able to put the const keyword
there.
Thanx
L.D.
Author: rmartin@rcmcon.com (Robert Martin)
Date: Fri, 13 Aug 1993 19:46:52 GMT Raw View
ldang@speech1.NoSubdomain.NoDomain (Student) writes:
> class foo {
> static int x;
> static int y;
> int z;
> public:
> static void f() const; // compiler complains
> ....
> };
>In foo::f(), I don't change neither foo::x nor foo::y, so why
>can't I declare foo::f() "const" ?
static functions do not operate on any particular instance of a class. So
they cannot be considered 'const'.
Consider.
class A
{
public:
void c() const;
void n();
static void k();
};
A nca;
const A ca;
ca.c(); // ok
ca.n(); // Error, cant use non const function on const object.
A::k(); // k is not invoked on a particular object,
Now, you may say: "Yes, but I can write this:" ca.k(); "Shouldn't this
be an error of k is non-const?"
No, because ca.k() simply degenerates to A::k(). k() is not being
called on the object 'ca'.
--
Robert Martin | Design Consulting | Training courses offered:
Object Mentor Assoc.| rmartin@rcmcon.com | Object Oriented Analysis
2080 Cranbrook Rd. | Tel: (708) 918-1004 | Object Oriented Design
Green Oaks IL 60048 | Fax: (708) 918-1023 | C++
Author: andy@aim1.aztec.co.za (Andrew Cooper)
Date: 13 Aug 93 10:43:29 GMT Raw View
ldang@speech1.NoSubdomain.NoDomain (Student) writes:
>Hi experts !
>Let's have the following class
> class foo {
> static int x;
> static int y;
> int z;
> public:
> static void f() const; // compiler complains
> ....
> };
>My question is :
>In foo::f(), I don't change neither foo::x nor foo::y, so why
>can't I declare foo::f() "const" ?
>Conceptually, I should be able to put the const keyword
>there.
According to the ARM, a function declared const has a this pointer with
the type const X* const. A static member function does not have a this
pointer, so this may be a reason for the compiler error. Without the
actual error message, I'm merely guessing at the problem. Perhaps some
more detail would be helpful.
Regards
Andrew
--
-------------------------------------------------------------------------------
Andrew Cooper Tel: +27 21 244350 (w)
Realtime Computer Services +27 21 6856858 (h)
Cape Town email: andy@aim1.aztec.co.za