473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
3 2291
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
8
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the...
10
by: Simon | last post by:
Hi, I have something like // // common.h const unsigned long m_dwStyle = 0x123; // // common.h
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
3
by: chsalvia | last post by:
In generic programming, static member functions and functors seem to be very useful. I've noticed that some libraries take the approach of using templated static member functions to provide...
6
by: subramanian100in | last post by:
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. In the same way, is...
3
by: tomPee | last post by:
Hi, I have the following problem: I am trying to make some sort of base class menu that i can then use to derive other menu's from. Those menu's should then be able to interact with each other....
4
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.