By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 2,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

C++: Static member functions and 'const'.

P: n/a
The C++ language standard stipulates at section 9.4.1 that "[...] A static
member function shall not be declared const [...]"

The question is: what problem(s) could possibly exist in allowing static
member functions to be 'const' with the implication that they could not
modify static data members?
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Thu, 13 May 2004 07:57:30 -0700 in comp.lang.c++, "qWake"
<ma**@qWake.com> wrote,
The C++ language standard stipulates at section 9.4.1 that "[...] A static
member function shall not be declared const [...]"

The question is: what problem(s) could possibly exist in allowing static
member functions to be 'const' with the implication that they could not
modify static data members?


The problem would be yet another inconsistency in the C++ language.

No member function can be declared to not modify static data members.
That's not what a const member function means; it means that it cannot
modify the object (*this) and that it can be called on (*this) objects
that are const.

Also, sorry, I don't immediately see any great value to declaring that
the function cannot modify static members.

Jul 22 '05 #2

P: n/a
On Thu, 13 May 2004 07:57:30 -0700, "qWake" <ma**@qWake.com> wrote:
The C++ language standard stipulates at section 9.4.1 that "[...] A static
member function shall not be declared const [...]"

The question is: what problem(s) could possibly exist in allowing static
member functions to be 'const' with the implication that they could not
modify static data members?
Remember that, conceptually, a static data member is "owned by the class",
not by the user. The appearance of "const" in function declarations is like
a promise the function is making to the user that it will not alter one of
the user's variables. That may be a variable passed in as an argument, or
the object a non-static member function is being is being applied to.
There's no corresponding "danger" with respect to a user's "own data" when
we talk about whether or not a function alters static data of the class.
That data belongs to the class, and the function isn't obliged to make any
promises to anyone about what it does with it, nor would there be any
conceivable benefit to its being able to do so.
-leor


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3

P: n/a
On Thu, 13 May 2004 07:57:30 -0700, "qWake" <ma**@qWake.com> wrote:
The C++ language standard stipulates at section 9.4.1 that "[...] A static
member function shall not be declared const [...]"

The question is: what problem(s) could possibly exist in allowing static
member functions to be 'const' with the implication that they could not
modify static data members?


There would be no way of propogating the constness, and preventing
callers from calling non-const static methods - it only applies at a
single level and therefore is of limited use. e.g.

const Foo foo;
foo.f(); //f must be a const member
foo.static_f(); //no requirements on foo,
//even were const statics allowed.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.