Advantages of using static member functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sanketbarot
    New Member
    • Sep 2006
    • 30

    Advantages of using static member functions

    Hello Friends,


    What is the advantage of defining a function as Static as a class member.

    Like

    Class Name
    {

    public :

    static void fun()

    }

    so what is the significance of defining a function as static.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    A static member function can only access static data members of the class, it can not access instance data members.

    A static member function can be call with have to instantiate the class via the class name

    Code:
    #include <iostream>
    
    using namespace std;
    
    class myClass
    {
    protected:
        static int iCount;
    
    public:
        myClass()
        {
            iCount++;
        }
        ~myClass()
        {
            iCount--;
        }
    
        static int GetInstantiationCount()
        {
            return iCount;
        }
    };
    
    int myClass::iCount = 0;
    
    int main()
    {
        cout << "Number of myClass instantiated: " << myClass::GetInstantiationCount() << endl;
    
        myClass *array = new myClass[500];
        
        cout << "Number of myClass instantiated: " << myClass::GetInstantiationCount() << endl;
    
        delete[] array;
    
        cout << "Number of myClass instantiated: " << myClass::GetInstantiationCount() << endl;
    
        return 0;
    }

    Comment

    • arne
      Recognized Expert Contributor
      • Oct 2006
      • 315

      #3
      Originally posted by sanketbarot
      Hello Friends,


      What is the advantage of defining a function as Static as a class member.

      Like

      Class Name
      {

      public :

      static void fun()

      }

      so what is the significance of defining a function as static.

      Static member functions are associated with the class, not with an object. One can use static member functions, whenever you have functionality (or data) that is not needed to be copied to all objects (e.g. since it is the same).
      A simple example would be the number of objects you have made from a class. This information would be stored to a static class member, since is is the same for all objects.
      Another advantage concerns the scope. Your fun() function in the example above is identified via
      Code:
      Name::fun
      and can thus not be confused with other function which may have the same name. This can be helpful in situations where the compiler would otherwise have problems to identify which function you want to call (think of template arguments as an example).

      Comment

      • sanketbarot
        New Member
        • Sep 2006
        • 30

        #4
        Thank you friends

        Comment

        • Shana
          New Member
          • Oct 2006
          • 20

          #5
          Hi frnd

          One of the advantage of static menber function in C++ is that it can help to count the number of objects (that uses static function )created in a class as static function will be shared by all the objects.

          Comment

          Working...