A static member function is a member function of a class. It does not belong to any object applied for by the class, but a function shared by all the members of the class.
Like static data members, static member functions are part of a class, not a part of an object. If you want to call a public static member function outside the class, use the class name and the domain operator "alias ". For example
Box volume ();
In fact, static member functions can also be called by object names, such
A. Volume ();
But this does not mean that this function belongs to object A, but only uses the type.
Static member functions are used to process 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 the this pointer. A static member function does not belong to a certain object and has nothing to do with any object, the static member function does not have the this pointer. It is determined that the static member function cannot access non-static members in this class.
In C ++ programs, static member functions are mainly used to access static data members rather than non-static members. Assume that a static member function has the following statements:
Cout
Cout <width <Endl; // If width is a non-static data member, it is invalid
However, it is not absolutely not allowed to reference non-static members in this class, but it is not allowed to perform default access because it cannot know which object to look. If you must reference non-static members of this class, you should add the object name and the member operator ".". For example
Cout <A. width <Endl; // reference non-static members in the Class Object.
Assume that a has been defined as a box object and is valid in the current scope, this statement is legal.
Static member functions