Understanding static member functions)

Source: Internet
Author: User

Before proceeding to the subject, I think we should first propose two self-defined terms: class-level members and object-level members ). I don't know if such a term already exists in the C ++ field, but I think these two terms can help us understand static member function very well. Here, someone may want to correct me: the original member (member) is a unique part of the class, and it is also divided into class-level and Object-level. However, I believe that after reading this article, if your previous understanding of the static member function turns into the current "It turns out to be the same thing", you will think this is a great thing. The so-called class-level Member means that the member belongs to the class and has only one class. The so-called object-level Member means that the member belongs to the object, as long as the number of objects in this class is generated, the number of such members will be generated. It doesn't matter if you still don't understand what it means. Let's take a look at it and you'll understand it. (actually, there's nothing, it's just the difference between static member and non static member, I think this is more conducive to understanding ).

Non static member is an object-level member function. That is to say, each object belonging to this class generates a member of its own. Static member is a Class-level member. That is to say, no matter how many objects the class generates, such members only generate one. Why does static member function only allow access to static member data? It is because static member data belongs to class-level member data, non static member data belongs to object-level member data, and static member function belongs to class-level member functions, non static member function is an object-level member function. A class-level member function can only access class-level member data. Because class-level members are shared, and Object-level members are private, private data can only be read by private functions. (do not confuse the sharing and private relations with the public and private relations in the gray declaration, private refers to the object private, while the latter refers to the class private ).

If you still have a poor understanding of why class-level members can only access the data of class-level members, then I will tell you why private object data cannot be accessed by shared functions. Declaration of the following classes:
Class Point
{
Public:
Void OutPut ();
Static void Init (int, int );

Private:
Int m_x;
Static int m_y;
};
Int Point: m_y = 10; // initialize static member data
Point ptA, ptB;

If the following functions are defined:
Void
Point: OutPut () // correct
{
Cout <"x =" <x <endl
<"Y =" <y <endl;
}
Void
Point: Init (int x, int y)
{
M_x = x; // error. m_x indicates object-level member data, and Init () indicates class-level member functions.
M_y = y; // correct. Both m_y and Init () are class-level members.
}

Because both ptA and ptB generate m_x of the object itself (if more Point objects are defined, more will be generated ). There is no doubt that OutPut () is correct, because ptA and ptB will also generate their own OutPut (), and the object's own function access belongs to the object's own data. However, Init () only generates one, while ptA and ptB both belong to their own m_x (they do not infringe upon each other), Init () how do you know which m_x should be accessed? (to be accurate, how should the compiler know which m_x should be accessed ), or is m_x of all Point objects accessed? (isn't that a violation of privacy ).

Static member function has another feature that is rarely mentioned in general language textbooks. When we use MFC, we often use the following Class Name :: class member functions ). To use such a call method, a condition is required, that is, this class member function must be a static member function. If you do not understand the preceding explanation, you will not think of this. So why must static member functions be called in this way? In fact, the principle is the same as above, because static member functions only generate one. If you still can't think of why, let me explain everything in reality! If the declaration of the class is used as follows:

Point: Init (5, 5); // correct (only the call method is discussed here, and its definition is not discussed correctly), because Init () is a Class-level member function.
PtA. Init (3, 3); // correct. Since Init () is shared, ptA can also be used.
Point: OutPut (); // error. OutPut () is an object-level member function.
PtA. OutPut (); // correct, object-level function call

As mentioned above, both ptA and ptB generate their own OutPut (). How does the compiler know that the OutPut () of the object should be called, object-level functions can only use object-level calling methods. Init () is well understood, because there is only one Init () by the handler. It is reasonable to call it at the class level or at the object level.

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.