C ++ BASICS (1)

Source: Internet
Author: User

InC ++Class, that is, the class of the custom type (referred to as the class), which has no difference with the structure at all, but is explained in the next article ), the reason for providing a class is actually because C ++ is extended from C, and the class is a very important concept proposed by C ++, the keyword struct is reserved only for compatibility with the C language.

For the time being, we can first consider that the great progress of the structure of the class is that the concept of member functions can be added, although the structure can also have member functions). Before learning about member functions, we should first look at a semantic requirement.

Operations and resources

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 manifested that a resource has operated on another resource and is temporarily called an 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:

 
 
  1. struct Monster { float Life; float Attack; float Defend; };  
  2. 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:

 
 
  1. 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 is still mapped to the address Code address in the memory), but because it is an offset type, that is, relative, that is, incomplete, therefore, function operators cannot be applied to it, such:

 
 
  1. ABC::AB( 10 ); 

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:

 
 
  1. void ABC::AB( long d ) { this->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 above. At least the name of the function is ABC: AB, which 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, and its type is ABC *, which is automatically generated by the compiler. That is, the above function definition is actually equivalent

 
 
  1. void ABC::AB( ABC *this, long d ) { this->a = d; } 

The reason why the declaration of this parameter should be omitted, and the compiler is responsible for the purpose of reflecting the semantics mentioned above, that is, the meaning of the member, in the Code) is also called ABC :: AB is the offset type of the function type. It is relative to this parameter:

 
 
  1. ABC a, b, c;   
  2. a.ABC::AB( 10 );   
  3. b.ABC::AB( 12 );   
  4. 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 the above

 
 
  1. a.ABC::AB( 10 ); 

Like member variables, since the left and right types must correspond to each other, you can

 
 
  1. a.AB( 10 ); 

Note that When ABC: AB is defined above, it should be written in the function body.

 
 
  1. this->a = d; 

Same as above, because the type must correspond to the relationship, that is, this must be a pointer to the corresponding custom type, so you can also omit this-> writing, and then there are

 
 
  1. 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, that is, C ++ does not provide any keyword or type modifier to indicate that the returned type VC provides the _ thiscall type modifier internally, but it cannot be used when writing code, only 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 to indicate that the member operator can be applied ), 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

 
 
  1. a.AB( 10 ); 

The address on the left of the member operator should be passed in as the first parameter of the function call, and the other parameters given in the function operator should be passed in. 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, without modifying numbers, you must be able to cope 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, you can

 
 
  1. ABC::AB; 

But not

 
 
  1. ABC::a; 

Because the latter is the offset value. Based on the type match, it is easy to know that there are also:

 
 
  1. Void (ABC: * p) (long) = ABC: AB;
  2. Or
  3. Void (ABC: * p) (long) = & ABC: AB;

Then there will be:

 
 
  1. 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. This code is meaningless, here, we only need to deepen our understanding of member functions.

 
 
  1. struct ABC { long a; void AB( long ); };  
  2. void ABC::AB( long d )  
  3. {  
  4.   this->a = d;  
  5. }  
  6. struct AB  
  7. {  
  8.   short a, b;  
  9.   void ABCD( short tem1, short tem2 );  
  10.   void ABC( long tem );  
  11. };  
  12. void AB::ABCD( short tem1, short tem2 )  
  13. {  
  14.   a = tem1; b = tem2;  
  15. }  
  16. void AB::ABC( long tem )  
  17. {  
  18.   a = short( tem / 10 );  
  19.   b = short( tem - tem / 10 );  
  20. }  
  21. void main()  
  22. {  
  23.   ABC a, b, c; AB d;  
  24.   ( c.*( void ( ABC::* )( long ) )&AB::ABC )( 43 );  
  25.   ( b.*( void ( ABC::* )( long ) )&AB::ABCD )( 0XABCDEF12 );  
  26.   ( d.*( void ( AB::* )( short, short ) )ABC::AB )( 0XABCD, 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

 
 
  1. a = short( tem / 10 ); 

Start execution, and the parameter tem maps the first address of the memory that passes the parameter, and then uses the long type to explain and get the tem as 43, and then runs.

Note:

 
 
  1. b = short( tem - tem / 10 ); 

Actually

 
 
  1. this->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 in function AB: ABC ), so this-> B normal ABC structure does not have B member variable), and B's offset is 2, therefore, after the preceding statement is executed, the result 39 is stored in the c address and the memory corresponding to 2, and the 16-bit binary data is stored in the short type.

For

 
 
  1. a = short( tem / 10 ); 

Do the same thing, so the value of c. a 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.

Next Article>

Related Article

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.