473,379 Members | 1,257 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 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 14824
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

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

Similar topics

4
by: 0to60 | last post by:
I don't know if I have that terminology right, but does anyone know if static member functions (or free standing functions for that matter) are any less overhead than actual member functions that...
3
by: exits funnel | last post by:
Hello, One of the problems at the end of Chapter 14 in Bruce Eckel's thinking in C++ reads as follows: Create a class with two static member functions. Inherit from this class and redefine...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
5
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
10
by: shanknbake | last post by:
I'm getting the following compile-time error: error C2352: 'Person::getCount' : illegal call of non-static member function Here is my getCount function declaration:...
6
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or...
6
by: subramanian100in | last post by:
why can't a static member function be declared as const ? We can declare a non-static member function as const, to indicate that it does not modify the non-mutable data members. In the same way, is...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.