const member fn changes static data

Discussion in 'C++' started by trying_to_learn, Nov 15, 2004.

  1. 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();
    }
     
    trying_to_learn, Nov 15, 2004
    #1
    1. Advertising

  2. "trying_to_learn" <> wrote in message
    news:cn9hef$hno$...
    > 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
     
    Ivan Vecerina, Nov 15, 2004
    #2
    1. Advertising

  3. trying_to_learn

    Jason Heyes Guest

    "trying_to_learn" <> wrote in message
    news:cn9hef$hno$...
    > 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?
     
    Jason Heyes, Nov 15, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rakesh Sinha
    Replies:
    4
    Views:
    1,868
    Rakesh Sinha
    Jan 13, 2005
  2. Dave
    Replies:
    10
    Views:
    35,371
    Ron Natalie
    May 22, 2005
  3. Javier
    Replies:
    2
    Views:
    603
    James Kanze
    Sep 4, 2007
  4. er
    Replies:
    3
    Views:
    398
  5. fungus
    Replies:
    13
    Views:
    927
    fungus
    Oct 31, 2008
Loading...

Share This Page