const member fn changes static data

T

trying_to_learn

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();
}
 
I

Ivan Vecerina

trying_to_learn said:
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
 
J

Jason Heyes

trying_to_learn said:
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?
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top