473,465 Members | 1,399 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

static member function and const

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
there a provision to indicate that a static member function does not
modify the static data members but only uses their values ?

Kindly explain.

Thanks
V.Subramanian

Nov 12 '07 #1
6 3729
On Nov 11, 10:53 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
why can't a static member function be declared as const ?
Because it wouldn't make any sense. A method is essentially the same
as a static function with one additional parameter, which is the
"this" variable. Making a method const just specifies that "this" is
const. It doesn't have anything to do with whether the function
changes any class data, per se, just whether "this" is const.

If you really *need* to specify that it can't change some data, put
the data in a separate container class and make the static function
take a const reference to the data. But I think it's more likely that
it's just not necessary. You write const methods so you can use them
with const instances, but that's irrelevant with static data.

Nov 12 '07 #2
On 2007-11-11 22:53:45 -0500, "su**************@yahoo.com, India"
<su**************@yahoo.comsaid:
why can't a static member function be declared as const ?
Because a static member function does not hold an implied object of
which its member variables can be modified (or its non-const methods be
called).
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
there a provision to indicate that a static member function does not
modify the static data members but only uses their values ?
Yes. If you don't want a static member function to modify non-mutable
data (and to call non-const methods), then just declare the input
parameters const like this:

class T
{
public:
// func won't be able to modify 't'
static void func(const T &t);
};

--

-kira

Nov 12 '07 #3
Debating possible future meanings doesn't help answer the OP's
question.
Like you said, we have to deal with the language as-is.

Nov 12 '07 #4
On Nov 12, 6:13 am, "Alf P. Steinbach" <al...@start.nowrote:
* Kibiz0r:
On Nov 11, 10:53 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
why can't a static member function be declared as const ?
Because it wouldn't make any sense.
This is a common misconception where any missing language feature is
discussed.
Wouldn't it be more accurate to say that the statement doesn't
have any real meaning. It's an almost meaningless phrases used
to lead into the real argument. If it's not followed up by
further argument, it's vacuous. If it is, it means whatever the
further argument says it means.

In this case, the poster was quite clear: allowing the keyword
const behind a static function makes no sense with regards to
the current semantics associated with const applied to a
function. Obviously, if you want to propose other semantics,
then someone opposing them would have to offer other arguments.
E.g., it's also often offered as a purported explanation for
the lack of 'virtual static' member function and for the lack
of 'virtual' constructors.
And for just about everything. That's a frequent case for more
or less vacuous lead-ins. They're just rhetoric (not
necessarily in the bad sense).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 12 '07 #5
"su**************@yahoo.com, India" <su**************@yahoo.comwrote in
news:11*********************@q5g2000prf.googlegrou ps.com:
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
there a provision to indicate that a static member function does not
modify the static data members but only uses their values ?

Kindly explain.

Thanks
V.Subramanian

I believe the biggest issue is that it isn't needed. Let me explain that.
Const member functions are required to flag member functions which are
callable on a const object. Note that this leaves you with the ability to
write to static member data and global data, just not member data. With
static functions, this requirement doesn't exist because there is no const
class data.

If you are looking for some form of protection against malicious
programmers, then you will need to look at a different language.
const_cast<and other mechanisms like that allow the author to do what he
wants (pretty much anyway).

Have you run into a lot of static member functions that surprised you by
modifying static member data? That is, in my experience, the function name
gives a good clue as to whether it modifies data or not.

joe
Nov 13 '07 #6
On Sun, 11 Nov 2007 19:53:45 -0800, su**************@yahoo.com, India wrote:
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
there a provision to indicate that a static member function does not
modify the static data members but only uses their values?
A bit off-topic, but if you want to indicate that a function depends
strictly on its parameters and does not access any properties that
weren't passed as parameters, in GCC there's an extension called
"pure" that flags that. Example:

int timestwo(int n) __attribute__((pure));
int timestwo(int n) { return n*2; }

It helps the optimizer because now it knows that
timestwo(5)+timestwo(5) can be optimized as timestwo(5)*2
because the function does not have side effects.
There is no standard feature equivalent to this.

--
Joel Yliluoma - http://iki.fi/bisqwit/
Nov 19 '07 #7

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
1
by: Seb | last post by:
Is this efficient for a header file and a class? Does a 'static local' variable get created for each instance or include of the header file? Also, 'return _type().ToString();' does not seem to...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
2
by: trying_to_learn | last post by:
while seeing an example i was surprised to see that a const member function is allowed to change a static data member as shown below. I am trying to reason why.... and my guess is that static data...
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...
2
by: mimi | last post by:
Hi,all. The section 13.5.1 of the <C++ primer 3rd editionsays, a static member frunction may not be declared as const or volatile. I could not explain to myself why? The constness seems to be...
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....
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.