To understand a static member function (the static function) __ functions

Source: Internet
Author: User
Tags function definition

Before I turn to the point, I think we should first put forward two of my own custom terms: class level members and object level members. I don't know if there is such a term in the C + + domain, but I think these two terms can help us understand the static member function well. There are people here who want to correct me: the original members (member) is a unique part of the class, but also what class-level and object-level. But I believe that if you had finished reading this article, you would have thought it would be a great two things if you had made that kind of smattering of the static member function become the "original thing". A class-level member means that the member belongs to a class, and a class has only one, and the so-called object-level member means that the member belongs to the object itself, so long as the number of objects in the class produces the number of such members. If you still do not understand the meaning of it, it does not matter, look down, slowly you will understand (in fact, nothing, but also static member and non static member of the difference, but I think it is more conducive to understanding it).

Non static member is part of an object-level membership function, that is, each object that belongs to that class will produce a member of its own. The static member is a class-level member, that is, no matter how many objects the class produces, the members only produce one. Why the static member function can only access static member data. Because static member data belongs to class-level members, non static member data belongs to the object-level members, and the static member function is class-level, non static A function is a member function that belongs to an object level. Only class-level member data can be accessed for a class-level member function. 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 here with the private relationship and the public in the Gray Declaration), where the private is the object private, and the latter refers to the class private.

If the above explanation is still not very good to understand why class-level members can only access class-level member data, I will actually tell you why private object data cannot be accessed by shared functions. In the case of a statement of the following categories:
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 you have the following function definition:
void
Point::output ()//correct
{
cout << "x=" <<x <<endl
<< "y=" <<y <<endl;
}
void
Point::init (int x, int y)
{
m_x = x; Error, M_x is an object-level member data, Init () is a class-level member function
m_y = y; Correct, M_y and init () are class-level members
}

Because PTA and PTB both produce m_x that belong to the object (if more objects are defined, more will be generated). For output () is no doubt, because PTA and PTB will also produce their own output (), the object of their own function access belongs to the object of their own data, this is for granted. But Init () will only produce one, and PTA and PTB have their own m_x (each non-aggression), init () How to know should visit that m_x it (accurate point should say compiler how to know should visit that m_x), or for all point of the object of M_ X is accessed (that's the invasion of privacy).


The static member function has another feature that is rarely mentioned in general language textbooks, and we often use MFC to call a function with the "Class Name:: Class Membership function" (I'm not saying that only MFC has such a call). To use such a calling method, you need to have a condition, that is, the class member function must be a static. You wouldn't have thought of that if you didn't understand the previous explanation. So why must the static member function be invoked like this, in fact the same principle as before, because the static member function only produces one. If you still can't imagine why, then I will use the actual to explain everything. Follow the declaration of the previous class, if you have the following usage:

Point::init (5, 5); Correct (here only discusses its calling method, not discussing whether it's defined correctly), because Init () is a class-level member function
Pta.init (3, 3); Correct, since Init () is shared, PTA nature can also use
Point::output (); Error, OutPut () is an object-level member function
Pta.output (); Correct, Object-level function call

As mentioned earlier, PTA and PTB will produce their own output (), the compiler how to know should call that object output (), object-level functions can only use the object-level call method. This is well understood for Init (), since Init () has only one from beginning to end, regardless of class-level invocation or object-level invocation.  

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.