Static member functions

Source: Internet
Author: User

A static member function is a member function of a class that does not belong to any of the objects requested by that class, but rather a function common to all members of that class.  
For example, there are 10 people in your class (10 for example, one by one classes), 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 impact is on the whole). So, I can say that basketball is
A member of the first class. So that is to say: the static member function is the member function of the class (because one or two classes can not be played), but this basketball is the most
After the return to the teacher, any private shall not occupy. Hope that you can understand that in fact in the machine structure inside the static members of the memory is the only
One, which is created when you declare him outside the class, but the spatial allocation of non-static functions is when you instantiate the object. I hope you can understand.
White, the first time in Baidu know inside answer, before are to check ....

As with static data members, static member functions are part of a class, not part of an object. If you want to invoke a public static member function outside of the class, use the class name and the domain operator. Such as
Box∷volume ();
Static member functions are also allowed to be called through the object name, as shown in
A.volume ();
This does not mean that the function belongs to object A, but only the type of a.
A static member function is intended to handle static data members.
Can say,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 is not related to any object, and the static member function does not have the this pointer. This determines that a static member function cannot access non-static members in this class.
In C + + programs, static member functions are primarily used 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 width is non-static data member, illegal
However, it is not absolutely impossible to refer to non-static members in this class, but not to make default access, because there is no way to know which object to look for. If you must refer to a non-static member of this class, you should add the object name and the member operator ".". Such as
cout<<a.width<<endl; Referencing a non-static member in this class of object a
This statement is valid if a has been defined as a box class object and is valid within the current scope.
1#include <iostream>2 3 using namespacestd;4 5 classStudent//defining the Student class6 {7      Public:8 9Student (intNintAfloats): num (n), age (a), score (s) {}//Defining ConstructorsTen  One voidTotal (); A  - Static floatAverage ();//declaring a static member function -  the Private: -  - intnum; -  + intAge ; -  + floatscore; A  at Static floatSum//static data members -  - Static intCount//static data members -  - }; -  in voidStudent∷total ()//defining non-static member functions - { toSum+=score;//Cumulative Total +  -count++;//cumulative number of people who have been counted the } *  $ Panax Notoginseng floatStudent∷average ()//defining static member functions - { the     return(sum/count); + } A  the  +  - floatstudent∷sum=0;//initializing static data members $  $ intStudent∷count=0;//initializing static data members -  -  the  - intMain ()Wuyi { theStudent stud[3]={//defining an array of objects and initializing them -  WuStudent (1001, -, -), -  AboutStudent (1002, +, +), $  -Student (1005, -,98) - }; -  A intN; +  thecout<<〃please Input the number of Students:〃; -  $cin>>n;//Enter the average number of students who need to be asked before the  the   for(intI=0; i<n;i++)//Call the total function 3 times the  the stud[i].total (); -  inCout<<〃the Average score Of〃<<n<<〃students is〃<<student∷average () <<Endl; the  the //calling a static member function About  the return 0; the  the } +  - run the result as the BayiPlease input the number of students:3 the  theThe average score of3Students is 82.3333

(1) The Stud object array is defined in the main function, in order to make the program concise, it is only defined that it contains 3 elements and holds 3 students ' data 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 members sum (total score) and count (the number of students who need to be counted) are defined, because the values of the two data members need to be cumulative, they are not only part of an object element, but are shared by each object element, as you can see: Their values are constantly changing and are the same regardless of which object element they are, and always do not free up memory space.
(3) Total is a member function of the public, whose purpose 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 it can reference a static data member in a class. Score is a non-static data member, and 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 scores.

Static member functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.