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

Q

qWake

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?
 
D

David Harmon

On Thu, 13 May 2004 07:57:30 -0700 in comp.lang.c++, "qWake"
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.
 
L

Leor Zolman

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
 
T

tom_usenet

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top