Home  >  Article  >  Web Front-end  >  POSITION usage explanation

POSITION usage explanation

巴扎黑
巴扎黑Original
2017-06-27 13:39:251919browse

POSITION is a data type that is often used in the MFC template class library. We can see from its definition that, in fact, it is a pointer.
//abstract iteration position
struct POSITION { };
typedef POSITION* POSITION;
The comment given by MFC is: an abstract iteration position. Since it is abstract, it means that no concrete is given. Data types to meet the needs of different template parameters.
In CList, POSITION is often used as a reference parameter, or as a return value. Below, we will give two simple examples to help you understand this data type, but before that, I want to be verbose, because many people are skeptical about the empty structure of POSITION. It seems that This becomes a barrier to their understanding. Since it is an empty structure, how can it be used to define a pointer?
In fact, the C++ compiler will not regard an empty structure as completely empty. The compiler will allocate 1byte of memory for it. In fact, to put it bluntly, the POSITION structure is equivalent to an unsigned char type. , so POSITION is equivalent to unsigned char, which is the BYTE type in Windows, so POSITION has an equivalent form, which is BYTE*.
We take two typical member functions in CList as the focus of the explanation. Of course, everyone should know that CList is a linked list data structure. Let’s look at these two functions:
1. POSITION GetHeadPosition() const;
Obviously, this function is used to get the head of the linked list, and its return value is a POSITION, which is actually a pointer. Who does this pointer represent? Of course, it is the pointer to the head of the linked list in the linked list that you have always wanted. This pointer comes from new, and you must not delete this pointer without authorization.
2. TYPE& GetAt(POSITION position);
This function looks strange. Its only input parameter is a pointer. In fact, it may be the head node pointer you just obtained using GetHeadPosition. The meaning of this function is that you need to provide the address of a node, and then CList will traverse the entire chain to find and return node data that matches this address.
So much nonsense, in one word, all the results are achieved. In other words:
POSITION, you can think of it as a student ID, with it, you can find any class students there.
POSITION is used in MFC to store the index of various List or Array objects to facilitate identifying the position of elements during traversal. In fact, it is a 32-bit value, and its content may be a pointer or the Index of an array.


The above is the detailed content of POSITION usage explanation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Use of HTML Map tagsNext article:Use of HTML Map tags