Results 1 to 6 of 6

Thread: static

  1. #1

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Unhappy static

    sorry to ask again, but what are static variables?? I dont understand what they are and they mean...
    Thanks
    Amon Ra
    The Power of Learning.

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    static variables can only be accessed within one module. So if you have a variable like "static int x" declared in the file like "a.cpp". It is declared as static so you wont be able access it if it is from another source moduel like "b.cpp"

    Simply:

    first file:

    a.cpp
    -------

    static int x;

    x = 32;
    cout<<x

    second file:

    b.cpp
    ------

    cout<<x; //ERROR

    ---------------

    SO, you cannot access x from a.cpp because it is declared as static in there

    Hope that helps
    Baaaaaaaaah

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Also when using classes, a static member variable is one that is shared among members of the class, and must be initialised at file scope:
    Code:
    class NewClass {
    public:
        static int m_iShared;
    };
    
    NewClass::m_iShared = 5;
    Then static member functions, which don't have a this pointer and are not allowed to reference any non-static member variables or functions:
    Code:
    class OtherClass {
    public:
        static int MyStaticFunction(int x) {
            return x + 1;
        }
    };
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Static variables are basically global variables scoped under classes, they are allocated when the app starts and destroyed when the app ends. The usefullness of static variables is that members functions can access them directly, private or public while only public static variables are accessible from outside, using scope operator :: Static functions are scoped similarily. Public static members aren't either directly under a global namespace keeping your code at least organized, but prefer to keep them private as often as possible. Public static members can be very usefull when creating templates too but that's another story
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Zaei
    Guest
    And then there is the static variable declared in a function:
    Code:
    INT myFunc(INT x) 
    { 
       static INT y = 0;
       return x*y++;
    }
    The first call to this function would start with y being set to 0, and end with it as 1. The next call, it would start as 1, and end as 2. The third, it would start as 2, and end with 3, etcetera, etcetera.

    Z.

  6. #6

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Smile

    Thanks a lot again!!Much clearer now=)
    Amon Ra
    The Power of Learning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width