The Real use of static

This is a discussion on The Real use of static within the C++ Programming forums, part of the General Programming Boards category; Hello, Why should i use static function and static members?What is their real usage? Please help me. Thanx in advance, ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    17

    Cool The Real use of static

    Hello,
    Why should i use static function and static members?What is their real usage?

    Please help me.

    Thanx in advance,
    John.
    codeffects software solutions

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    A workaround globals
    Woop?

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    > A workaround globals
    Yep. Plus unlike regular globals, you have some idea as to what the global is used for fairly quickly. Its parallel to using namespaces to keeping order among your global variables.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Why should i use static function and static members?What is their real usage?
    Sometimes it just doesn't make sense for every object to have a local copy of a data member. Take an object counter for example:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class test {
      static int class_n;
    public:
      test() { cout<<"There are "<< ++class_n <<" objects active"<<endl; }
      ~test() { cout<<"There are "<< --class_n <<" objects active"<<endl; }
    };
    
    int test::class_n = 0;
    
    int main()
    {
      test a, b, c;
    }
    Such a thing is common and useful, but it would be ugly and unsafe without static members. The same goes for static member functions. There are times when it doesn't make sense to define an object just to call a member function if the function doesn't really need the object to exist. Take a default value for example:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class test {
      static string default_path;
      string path;
    public:
      test(string init = default_path): path(default_path) {}
      static void set_default_path(string init);
      string get_path() const { return path; }
    };
    
    string test::default_path = "/my/stuff/";
    
    void test::set_default_path(string init)
    {
      default_path = init;
    }
    
    int main()
    {
      test a;
      cout<< a.get_path() <<endl;
    
      // But wait! What if we're using Windows?
      test::set_default_path("C:\\my\\stuff\\");
      test b;
      cout<< b.get_path() <<endl;
    }
    Sure, you could change the default path every time you create a new object, but this would be terribly annoying for clients who don't use a Unix style directory tree. So the static member function changes the default path class-wide. After calling it, any new objects will use the new default without needing to do anything special.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Also you can use static member function without instancing the class, to they are great if you want to control the access to the constructor, or just put a few related functions together in an elegant way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static keyword and headers. help me understand.
    By cmay in forum C Programming
    Replies: 4
    Last Post: 04-17-2009, 04:19 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. %16 with double
    By spank in forum C Programming
    Replies: 11
    Last Post: 03-05-2006, 10:10 PM
  4. [GLUT] Pointers to class methods
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 04:03 PM
  5. Thread and static objects
    By axr0284 in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2006, 01:11 PM

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