A static member function is a member function of a class that does not belong to any object of the class request, but is a function shared by all members of that class.
For example, you have 10 people in your class (10 for Class one by one), and the P.E. teacher gives you a basketball (static member function),
Each of you has a basketball (non-static member function), you are very stingy, your own ball can only shoot, if 5 to 5 dozen games,
Then you can only use that static basketball (everyone can use it, but the effect is on the whole). So, I can say that basketball is
A member of a class. So that is to say: the static member function is the member function of the class (because one or two classes can not play), but the basketball is the most
Still want to return to the teacher, any private must not occupy. Hope that you can understand, in fact, in the machine structure inside the static members of the memory is only
One is when you declare him outside the class, but the spatial allocation of non-static functions is created when you instantiate the object. I hope you can understand
White, the first time in Baidu know inside answer, before all is to check ....
As with static data members, static member functions are part of a class, not part of an object. If you want to call a public static member function outside of a class, use the class name and the domain operator to "get it". Such as
Box∷volume ();
Static member functions are actually also allowed to be invoked through object names, such as
A.volume ();
But that does not mean that this function belongs to object A, but only to the type of a.
The function of static member functions is to be able to handle static data members.
It can be said that the fundamental difference between a static member function and a non-static member function is that a non-static member function has a this pointer, and a static member function does not belong to an object, it has nothing to do with any object, and static member functions do not have this pointer. This determines that static member functions do not have access to non-static members in this class.
In C + + programs, static member functions are used primarily to access static data members without accessing Non-static members. If you have the following statement in a static member function:
cout<cout<<width<<endl; If the width is non-static data member, illegal
However, it is not absolutely impossible to refer to Non-static members in this class, except for default access, because you cannot know which object to look for. If you must refer to Non-static members of this class, you should add the object name and the member operator ".". Such as
cout<<a.width<<endl; Referencing non-static members in object A of this class
If a is defined as a box class object and is valid within the current scope, this statement is legitimate.
Example:
#include <iostream>
using namespace Std;
Class Student//Definition Student classes
{public:
Student (int n,int a,float s): num (n), age (a), score (s) {}//define Constructor
void Total ();
static float average (); declaring static member functions
Private
int num;
int age;
Float score;
static float sum; Static data members
static int count; Static data members
};
void Student∷total ()//defining Non-static member functions
{Sum+=score; Cumulative Total Score
count++; Cumulative number of people who have been counted
}
float student∷average ()//define static member functions
{return (Sum/count);
}
float student∷sum=0; Class for static data members.
int student∷count=0; Class for static data members.
int main ()
{Student stud[3]={//define object array and initialize
Student (1001,18,70),
Student (1002,19,78),
Student (1005,20,98)
};
int n;
Cout<<〃please input the number of Students:〃;
cin>>n; Enter the average score of the number of students required before
for (int i=0;i<n;i++)//Call 3 times Total function
Stud[i].total ();
Cout<<〃the Average score of〃<<n<<〃students is〃<<student∷average () <<endl;
Calling static member functions
return 0;
}
Run result is
Please input the number of Students:3↙
The average score of 3 students is 82.3333
Description
(1) A Stud object array is defined in the main function to make the program concise, defining it to contain 3 elements, and storing the data of 3 students respectively. The function of the program is to first ask the user to specify the total score of n students, and then the average score (n by the user input).
(2) in the student class, two static data member sum (total score) and count (total number of students who need to count) are defined. This is because the values of the two data members need to be cumulative, they are not belong to only one object element, but are shared by each object element, you can see: Their values are constantly changing, and they are the same regardless of the object element, and the memory space is never freed.
(3) Total is a public member function, the role is to add a student's performance to sum. A public member function can reference a generic data member (non-static data member) in this object, or a static data member in a class. Score are non-static data members, SUM and count are static data members.
(4) Average is a static member function that can directly refer to a private static data member (without having to add a class name or object name), and the function returns the average of the results.
(5) In the main function, refer to the total function to add the object name (now the object array element name), refer to the static member function average function to use the class name or object name.