Thread View: comp.std.c++
3 messages
3 total messages
Started by Bret Pehrson
Wed, 01 Nov 1995 20:34
Continue: static func() const; legal?
Author: Bret Pehrson
Date: Wed, 01 Nov 1995 20:34
Date: Wed, 01 Nov 1995 20:34
45 lines
1738 bytes
1738 bytes
A while back I asked whether the following was legal:
class base
{
static funkie(void) const;
};
I received two responses, both of which said (paraphrased):
It is not legal, and does not make sense because 'const' implies that
the this pointer is constant, and since 'static' members do not have
a this pointer, it is meaningless.
After some thought, I would like to repose the question:
Suppose:
class base
{
static void lumpy(void) const;
static int cLumps;
};
int base::cLumps = 0;
void base::lumpy(void)
{
++cLumps;
}
Now, since cLumps is a member of base, albeit static, making a static
member function 'const' would mean that it does not change available
class members (in this case, static member variables).
So, with this above sample, I can definitely see a legitimate need
for static const member functions.
Legal or not?
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Continue: static func() const; legal?
Author: Matt Austern
Date: Thu, 02 Nov 1995 02:30
Date: Thu, 02 Nov 1995 02:30
31 lines
2321 bytes
2321 bytes
In article <478k57$5gl@stellar.comnet.com> Bret Pehrson <BretP@strata3d.com> writes: > Now, since cLumps is a member of base, albeit static, making a static > member function 'const' would mean that it does not change available > class members (in this case, static member variables). > > So, with this above sample, I can definitely see a legitimate need > for static const member functions. The thing you have to remember is that static member variables really aren't particularly special in any way: from section 9.5.2 of the draft standard it's pretty clear that static member variables are almost exactly the same as global variables, except that the are in class scope instead of namespace scope, and they obey class access rules. There's no way to declare a function such that it's prohibited from modifying global variables; similarly, there's no way to declare a function such that it's prohibited from modifying static member variables. Static member variables are really a notational convenience more than anything else: pretty much anything you can do with static member variables, you can also do with global variables declared in a namespace. -- Matt Austern He showed his lower teeth. "We matt@physics.berkeley.edu all have flaws," he said, "and http://dogbert.lbl.gov/~matt mine is being wicked." [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Continue: static func() const; legal?
Author: Ralph Shnelvar
Date: Thu, 02 Nov 1995 23:29
Date: Thu, 02 Nov 1995 23:29
27 lines
1547 bytes
1547 bytes
matt@cyclops5.berkeley.edu (Matt Austern) wrote: >In article <478k57$5gl@stellar.comnet.com> Bret Pehrson ><BretP@strata3d.com> writes: > >There's no way to declare a function such that it's prohibited from >modifying global variables; similarly, there's no way to declare a >function such that it's prohibited from modifying static member >variables. That's a double pity. First, it is a pity that one cannot declare a function and say "You can't access global variables. Second, it is a pity that class static variables cannot be defined as untouchable. Thanks for pointing this counter-intuitive use of function() const out. I actually had to run a test compilation to believe you. Ralph --- [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads