Why can I modify my static class member?

This is a discussion on Why can I modify my static class member? within the C++ Programming forums, part of the General Programming Boards category; I wrote a class with a privated static memeber: i.e. static int x. I initialized the member outside the class. ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    16

    Unhappy Why can I modify my static class member?

    I wrote a class with a privated static memeber:
    i.e. static int x.

    I initialized the member outside the class.

    Now I have a function that reads like this:

    int& CLASSTYPE operator[](int);

    basically this function will return the class member.
    but when i created an object of the class say "obj"
    and wrote

    obj[integerhere]=5;

    it actually modified my static data member!!!

    What should I do? I'm lost and confused!

  2. #2
    Registered User actionbasti's Avatar
    Join Date
    Dec 2002
    Posts
    48
    Are you talking about const instead of static, maybe ??

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    The name "static" may seem to imply that it is constant, but this is not true. What it means is that there is only ONE instance of the variable in your whole address space. Any calls to the variable, from any instance of the class, go to the same memory location. This is useful in saving memory if something does not change between instances of a class.

    Another use of it is in non-member functions. If you need to retain data between calls to a function, declare a variable as static:

    Code:
    LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
        static int counter;
        switch (msg)
        {
            case WM_CREATE:
                 counter=0;
                 break;
            case WM_LBUTTONDOWN:
                 counter++;
                 break;
            default:
                break;
           }
          return DefWindowProc(hwnd,msg,wParam,lParam);
    }
    In that case, even if the function is called multiple times, only one instance of 'counter' will be created. Therefore, during each call, it retains the same value it had before.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use of 'static' in a class
    By h3ro in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2009, 11:48 AM
  2. How do i do this? (static structure inside class)
    By 39ster in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2008, 03:14 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Replies: 3
    Last Post: 10-10-2002, 08:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21