473,672 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const member fn changes static data

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 members really
dont belong to an object rather they belong to a class.
however, isn't const a strict thing ? : where the compiler says "if u
make this member function a const, i promise i wont let the function
change the state of an object", yet it lets a const function change
state of the object. why is this?

#include <iostream>
using namespace std;
class Obj {
static int i, j;
public:
Obj() {}
void f() const { cout << ++i << endl; }
void g() const { cout << ++j << endl; }
};
int Obj::i = 47;
int Obj::j = 11;

int main()
{const Obj Obj1;
Obj1.f();
cin.get();
}
Jul 22 '05 #1
2 1757
"trying_to_lear n" <no****@no.no > wrote in message
news:cn******** **@gist.usc.edu ...
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 members really
dont belong to an object rather they belong to a class.
however, isn't const a strict thing ? : where the compiler says "if u make
this member function a const, i promise i wont let the function change the
state of an object", yet it lets a const function change state of the
object. why is this? Static data members are not part of the state of any class instance.
They are more like global data that is encapsulated within the class,
for example to restrict access priviledges.
#include <iostream>
using namespace std;
class Obj {
static int i, j;
public:
Obj() {}
void f() const { cout << ++i << endl; }

The const applies to the state of the object on which f() is called.
As a static data member, 'i' does not belong to the object.
Consider the two additional members:
void foo(Obj& other) const;
void bar(const Obj& other);
In the body of foo and bar, when/why would constness be enforced
to the static data in Obj?
hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 22 '05 #2
"trying_to_lear n" <no****@no.no > wrote in message
news:cn******** **@gist.usc.edu ...
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 members really
dont belong to an object rather they belong to a class.
however, isn't const a strict thing ? : where the compiler says "if u make
this member function a const, i promise i wont let the function change the
state of an object", yet it lets a const function change state of the
object. why is this?

#include <iostream>
using namespace std;
class Obj {
static int i, j;
public:
Obj() {}
void f() const { cout << ++i << endl; }
void g() const { cout << ++j << endl; }
};
int Obj::i = 47;
int Obj::j = 11;

int main()
{const Obj Obj1;
Obj1.f();
cin.get();
}


Remember const affects the constness of the hidden "this" pointer. Think of

void f() const { cout << ++i << endl; }

as being equivalent to

void f(const Obj *this) { cout << ++Obj::i << endl; }

Does it make sense?
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3153
by: John Calcote | last post by:
I'm not sure is there's a g++ specific newsgroup... g++ is telling me there's a const-related problem with my source code and I just don't see it. Now, I don't know that it means much, but VC7 doesn't see the problem either: ----snip-snip-snip---- #include <string> #include <set>
3
3598
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 non-standard C++ or if the problem lies elsewhere. To summarize static const class members are not being accessed properly when accessed from a DLL by another external object file (not within the DLL). It only occurs when the static const...
14
2860
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
5
2454
by: partha.p.das | last post by:
Here is my class: class MyClass { public: MyClass(); ~MyClass(); private:
5
11318
by: John Goche | last post by:
Hello, I would like to know whethere there is a difference between a const variable and a static const variable inside a class. After all, if a variable is const in a class, the compiler can always optimize and turn it into a static const variable to save runtim memory/stack space since being const it cannot be changed. (?) Thanks,
4
6683
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
6
3752
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 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
15
7855
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
3
3501
by: NvrBst | last post by:
I'd like to do something like this but having a problem with the proper syntax in the constructor, maybe someone knows the correct syntax? ---MyClass.h--- #ifndef MYCLASS_H_ #define MYCLASS_H_ class MyClass { public:
0
8844
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8696
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7472
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6254
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4239
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2836
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 we have to send another system
2
2092
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1836
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.