Connecting Tech Pros Worldwide Help | Site Map

const member fn changes static data

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 09:53 PM
trying_to_learn
Guest
 
Posts: n/a
Default 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();
}

  #2  
Old July 22nd, 2005, 09:53 PM
Ivan Vecerina
Guest
 
Posts: n/a
Default Re: const member fn changes static data

"trying_to_learn" <nospam@no.no> wrote in message
news:cn9hef$hno$1@gist.usc.edu...[color=blue]
> 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?[/color]
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.
[color=blue]
> #include <iostream>
> using namespace std;
> class Obj {
> static int i, j;
> public:
> Obj() {}
> void f() const { cout << ++i << endl; }[/color]
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


  #3  
Old July 22nd, 2005, 09:53 PM
Jason Heyes
Guest
 
Posts: n/a
Default Re: const member fn changes static data

"trying_to_learn" <nospam@no.no> wrote in message
news:cn9hef$hno$1@gist.usc.edu...[color=blue]
> 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();
> }[/color]

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?


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.