C ++ starts from scratch (10) -- What is Class

Source: Internet
Author: User

C ++ from scratch (10) -- what is the original source of Class: Network

The first part of article 160 illustrates that the structure only defines the memory layout. class can also be written before the type definition operator, that is, the type custom type (class for short ), it is basically the same as the structure (only a small difference, as described in the next article). The reason why we need to provide a class is actually because C ++ is extended from C, the class is a very important concept proposed by C ++. It only retains the struct keyword for compatibility with the C language. However, the small difference mentioned in the brackets above is enough to show that the designers of C ++ define different semantics for structures and classes.
For the time being, we can first consider that the long progress of the structure of the class is more than the concept of member functions (although the structure can also have member functions). Before learning about member functions, we should first look at a semantic requirement.


Operation andResources

The program is mainly composed of operations and operated resources, and the operator is the CPU, which is normal, but sometimes there are some needs, it must be shown that a resource has operated on another resource (temporarily called operator). For example, in a game, it is often used to map monsters to Attack Players. Generally, this operation requires the operator to modify the operator or use the information recorded by the operator to complete the operation. For example, the attacking power of the monster determines the status of the player after the attack. This Semantics indicates that the operator has certain functions. To implement the preceding semantics, as previously mentioned in ing, the ing between monsters and players is structured as follows:
Struct Monster {float Life; float Attack; float Defend ;};
Struct Player {float Life; float Attack; float Defend ;};
The above attack operations can be mapped to void MonsterAttackPlayer (Monster & mon, Player & pla );. Note that the function name is expected to be used to represent the operator, but it is similar to the previous article to name the cross-river scheme sln, because the semantics should be represented by the Type rather than the function name. Therefore, C ++ provides the concept of member functions.


Member Functions

As before, writing the declaration statement of a function in a type definition operator defines a member function, as shown below:
Struct ABC {long a; void AB (long );};
The above defines a ing element -- the first variable ABC: a, the type is long ABC:; and declares a ing element -- the second function ABC: AB, the type is void (ABC:) (long ). The Type modifier ABC: modifies the ABC: AB function here, indicating that it is the offset type of the function type, that is, a relative value. But because it is a function, the meaning is different from the variable, that is, it still maps the address in the memory (the address of the Code), but because it is an offset type, it is relative, therefore, function operators such as ABC: AB (10) cannot be applied to it );. Here, it will be wrong, because ABC: AB is relative. Its relative content is not a memory address as a member variable, but a parameter of the structure pointer type. The parameter name is set to this, this is forcibly defined and will be explained later.
Note that because its name is ABC: AB, and the above is just to declare it, to define it, it is still the same as the previous function definition, as shown below:
Void ABC: AB (long d) {this-& gt; a = d ;}
Note that the name of the above function is ABC: AB, but like the member variables mentioned earlier, long ABC: a; cannot be written directly ;, in this case, you cannot directly write the Definition Statement of the function (at least the name of the function is ABC: AB does not comply with the identifier rules). Instead, you must first define the custom type through the type definition character, write the statement later.
Note that the keyword "this" is used above. Its type is ABC *, which is automatically generated by the compiler. That is, the preceding function definition is actually equivalent to void ABC: AB (ABC * this, long d) {this-& gt; a = d ;}. The reason why the declaration of this parameter should be omitted is that the compiler is responsible for reflecting the semantics mentioned above (meaning of members) in the code, which is also called ABC :: AB is the offset type of the function type. It is relative to this parameter:
ABC a, B, c; a. ABC: AB (10); B. ABC: AB (12); c. AB (14 );
The above uses the member operator to call ABC: AB. Note that after execution,. a, B. a and c. the values of a are 10, 12, and 14 respectively, that is, ABC: AB is called three times, but the value of this parameter three times is different through the member operator, then, the member variable a of the three ABC variables can be modified. Note that a. ABC: AB (10); is written above. Like member variables, a. AB (10) can also be used because the left and right types must correspond to each other );. Note that When ABC: AB is defined above, this-& gt; a = d; is written in the function body. Same as above, because the type must correspond to each other, that is, this must be a pointer of the corresponding custom type. Therefore, you can omit the writing of this-& gt;, and void ABC: AB (long d) {a = d ;}.
Note that the function of the member operator is not to return a number of the corresponding member variable type when it is a member variable, but to return a number of the function type, however, the difference is that this function type cannot be expressed in syntax, C ++ does not provide any keyword or type modifier to express the returned type (VC provides the _ thiscall type modifier internally for representation, but it is still not usable when writing code, but used internally by the compiler ). That is to say, when the right side of the member operator is a number of the function type offset type, a number of the function type is returned (indicating that it can be applied to the function operator ), the function type is the type given in the offset type, but this type cannot be displayed. A. AB returns a number, which is a function type. In VC, its type is void (_ thiscall ABC:) (long ), however, this type is invalid in C ++.
C ++ does not provide keywords like _ thiscall to modify the type, because this type requires the compiler to encounter function operators and member operators, such as. AB (10); transmits the address on the left of the member operator as the first parameter of the function call, and then transmits the other parameters given in the function operator. That is, this type provides the compiler with some information to generate the correct code for the function operator and member operator at the same time, instead of modifying numbers (modifying numbers requires dealing with all situations ). That is, the type is used to modify numbers, and this type cannot modify numbers. Therefore, C ++ does not provide a keyword similar to _ thiscall.
As before, because ABC: AB maps an address rather than an offset value, it can be ABC: AB; but it cannot be ABC: ;, because the latter is the offset value. Based on the type match, it is easy to know that there are also:
Void (ABC: * p) (long) = ABC: AB; or void (ABC: * p) (long) = & ABC: AB;
Then there is: void (ABC: ** pP) (long) = & p; (c. ** pP) (10.0f );. Brackets are added because the priority of function operators is higher. Recall that the previous article said that the conversion of pointer types only changes the type and the value remains unchanged (the next article describes the changes in the value). Therefore, the following code can be used, and this Code is meaningless, here, we only need to deepen our understanding of member functions.
Struct ABC {long a; void AB (long );};
Void ABC: AB (long d)
{
This-& gt; a = d;
}
Struct AB
{
Short a, B;
Void ABCD (short tem1, short tem2 );
Void ABC (long tem );
};
Void AB: ABCD (short tem1, short tem2)
{
A = tem1; B = tem2;
}
Void AB: ABC (long tem)
{
A = short (tem/10 );
B = short (tem-tem/10 );
}
Void main ()
{
ABC a, B, c; AB d;
(C. * (void (ABC: *) (long) & AB: ABC) (43 );
(B. * (void (ABC: *) (long) & AB: ABCD) (0XABCDEF12 );
(D. * (void (AB: *) (short, short) ABC: AB) (0 XABCD, 0XEF12 );
}
After the above execution, c. a is 0X00270004, B. a is 0X0000EF12, d. a is 0 XABCD, and d. B is 0 XFFFF. For function calls of c, because the address mapped by AB: ABC is directly converted to the type and used directly, the program will jump to AB :: a = short (tem/10) at ABC; start execution, and the parameter tem maps the first address of the memory for passing parameters, and then interpreted with the long type to get the tem as 43, and then executed. Note that B = short (tem-tem/10); actually this-& gt; B = short (tem-tem/10 );, the value of this is the address corresponding to c, but here it is considered to be AB * type (because it is in function AB: ABC), so this-& gt; B is normal (the ABC structure does not contain the member variable "B"), and the offset of B is 2. Therefore, after the preceding statement is executed, the result 39 is stored in the address of c and the corresponding memory of 2 is added, in addition, the 16-bit binary data is stored in the short type. Do the same thing for a = short (tem/10); so the final c. a value is 0X0027004 (decimal 39 to hexadecimal 0X27 ).
Similarly, for B's call, the program will jump to AB: ABCD, but when B's call code is generated, record the parameter 0XABCDEF12 in the memory of the passed parameter in the long format, and then jump to AB: ABCD. However, when AB: ABCD is compiled, the corresponding addresses of the tem1 and tem2 parameters are mapped based on the two short types. Therefore, it is easy to think that the value of tem1 is 0XEF12, the value of tem2 is 0 XABCD, but this is not the case. How to pass parameters is determined by the previously mentioned function call rules. The specific implementation details of function calls are described in C ++ from (15th, here, we only need to know that the member function ing is still the address, and its type determines how to use it, which will be explained later.


Description

I have already explained the meaning of declaration. Here, due to the new definition syntax of the member function definition rules, the meaning of declaration must be reconsidered. Note that you can place the definition of a function before the definition of the main function, and you do not need to declare that function. If you define a variable, you do not need to declare that variable. This means that the definition statement has the declaration function, but the Definition Statement of the above member function does not have the declaration function. The following describes the true meaning of the Declaration.
Declaration is a statement that requires the compiler to generate ing elements. The so-called ing element is the variables and functions described earlier. There are only three columns (or three fields ): type column, name column, and address bar (this column of the member variable type contains the offset value ). That is, the compiler generates a ing element every time it sees the declaration statement, and leaves the corresponding address bar blank, and then leaves some information to tell the connector -- this. obj file (the file generated after the compiler compiles the source file, for VC is. obj file) requires some symbols, find these symbols and then modify and improve this. obj file.
In retrospect, the symbol is a string used for communication between the compiler and the connector. Note that the symbol has no type, because the connector is only responsible for finding and perfecting the symbol (because the address bar of some ing elements is still empty. obj file), no syntax analysis, there is no type.
The definition requires the compiler to fill in the address bar that was previously declared not written. That is to say, the address corresponding to a variable is only known when it is defined. Therefore, the actual job of allocating memory on the stack is defined by the variable.

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.