471,831 Members | 1,364 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,831 software developers and data experts.

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.
Oct 19 '06 #1
4 14376
Banfa
9,065 Expert Mod 8TB
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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class myClass
  6. {
  7. protected:
  8.     static int iCount;
  9.  
  10. public:
  11.     myClass()
  12.     {
  13.         iCount++;
  14.     }
  15.     ~myClass()
  16.     {
  17.         iCount--;
  18.     }
  19.  
  20.     static int GetInstantiationCount()
  21.     {
  22.         return iCount;
  23.     }
  24. };
  25.  
  26. int myClass::iCount = 0;
  27.  
  28. int main()
  29. {
  30.     cout << "Number of myClass instantiated: " << myClass::GetInstantiationCount() << endl;
  31.  
  32.     myClass *array = new myClass[500];
  33.  
  34.     cout << "Number of myClass instantiated: " << myClass::GetInstantiationCount() << endl;
  35.  
  36.     delete[] array;
  37.  
  38.     cout << "Number of myClass instantiated: " << myClass::GetInstantiationCount() << endl;
  39.  
  40.     return 0;
  41. }
  42.  
Oct 19 '06 #2
arne
315 Expert 100+
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
Expand|Select|Wrap|Line Numbers
  1. Name::fun
  2.  
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).
Oct 19 '06 #3
Thank you friends
Oct 20 '06 #4
Shana
20
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.
Oct 22 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by exits funnel | last post: by
1 post views Thread by Bryan Parkoff | last post: by
15 posts views Thread by Samee Zahur | last post: by
5 posts views Thread by Tony Johansson | last post: by
6 posts views Thread by Olumide | last post: by
6 posts views Thread by subramanian100in | last post: by
reply views Thread by YellowAndGreen | last post: by
aboka
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.