C + + from scratch (10) - what category

  C + + from scratch (10) 

  – What type 

  Former chapter is the only definition of the structure of the memory layout of it, that type definition can be written at the former class, that is, since the definition of the type type type (type), and its structure is no difference (only small point distinction, next note), and is also to provide a class, the actual due from C + + is extended from C, which is a C + + class by itself a very important concept, only to the C language compatibility while retaining the struct this keyword.    But through the brackets in front of the small distinction also said that to the designers of C + + class definition for the structure and the different semantics, next note. 
  Could be considered temporary structure of the rapid progress than is the membership function of this multi-concept (although some members of the structure can also function), in the understanding of function prior to a semantic view of demand. 

  Operations and resources 

  Mainly by the operating procedures and the operation of the resources, is the executor of the operation of CPU, this is very normal, but sometimes there are some really needs is a need to demonstrate operation of the other resources Resources (temporarily called Operation) , for example, the game often is to attack the monster mapping audience.    The need for the operator, generally because of this operation also needed to be modified by the operator or operators recorded some of the information to complete the operation, such as the attack of the monster audience to decide the status of the attack.    Such semantics for the performance of certain functions of the operator.    In order to achieve the above semantics, as the original said a mapping, mapping monster and the first player for the structure, as follows: 
  (Struct Monster float Life; float Attack; float Defend;); 
  Struct Player (float Life; float Attack; float Defend;); 
  Attacks on the operation of the above can be mapped to void MonsterAttackPlayer (Monster & mon, Player & pla).    Note that the function of expectations to the operator performance, but said that the articles and the former programme will be named sln river, are a kind of priorities, because this type of semantic should be performance rather than function names.    To this end, C + + member function provides a concept. 

  Members function 

  And before, in the type definition in writing at the function declaration statements from members of the definition function, as follows: 
  (Long a struct ABC; void AB (long);); 
  On the above definition of a mapping elements - a variable ABC:: a, type of long ABC::; statement, as well as a mapping elements - ABC second function:: AB, type void (ABC::) ( long).    Type Xiuchifu ABC:: In this modified the function ABC:: AB, said its function for the type of shift types, is one relative value.    However, as is the function and significance of different variables, namely, the mapping is still in the memory address (the address of the code), it is offset types, that is, relative, that is not complete, so it is not its function Application Operators such as: ABC:: AB (10);.    Here there will be mistakes, because ABC:: AB is relative, the relative is not something members such as variable as a memory address, but a pointer type of structure parameters of certain parameters for this, which is the definition of force, and behind that. 
  As its name to the attention of ABC:: AB, and its top is only a statement, it is necessary to define it, and still before the function definition, as follows: 
  Void ABC:: AB (long d) (this-> a = d;) 
  It should be noted the above function name is ABC:: AB, but that chapter and the former members of the same variables, and can not directly write long ABC:: a; can not be written directly above a function definition language (at least function called ABC:: AB id not comply with the rules), and must be of type definition at "()" to define custom type, and then write, which will be behind the statement elaborated. 
  Note that the above use of this this keyword, type ABC *, automatically generated by the compiler, the above definition of the actual function equivalent to void ABC:: AB (* this ABC, long d) (this-> a = d; ).    The reason for this omission of the statement by the compiler to do is to be embodied in the code to the aforementioned semantic (ie members of the meaning), which is why ABC said:: AB is the type of function offset types, It is with this in terms of this parameter, how relative, as follows: 
  ABC a, b, c; a.ABC:: AB (10); b.ABC:: AB (12); c.AB (14); 
  Operators use of the above call ABC:: AB, attention to the implementation, aa, ba and the value of ca 10, 12 and 14 respectively, that is, three times called ABC:: AB, but the result of operator members of the three parameters of this not the same value, and thus to modify the members of the three variables ABC a variable.    Note that the above writing a.ABC:: AB (10), and members of the same variables, corresponding to about type, it can also a.AB (10);.    It should also be noted above in the definition ABC:: AB, in the in vivo function of writing this-> a = d;, ibid, corresponding to the type of relationship that this must be a corresponding definition of the types of indicators, it can also be omitted from this-> writing, which is void ABC:: AB (long d) (a = d;). 
  Note that the role of members of operators, no longer members of variables such as the return of members of the corresponding types of digital variables, but a function return types of figures, but this is the function of different types that are not used by the grammar, that is, there is no C + + provide any keyword or type Xiuchifu performance to the return of this type (VC house ___ thiscall Xiuchifu this type of conduct that, but still can not write code to use, internal use only compiler).    In other words, when members of the right to operate at the type of function is offset types of figures, a return to a function types of digital (that its function can be subject to operator), the function of the type of migration to the types of The type, but not this type of performance.    A.AB that will be returned to a figure, the figure function type, in its internal VC type void (__thiscall ABC::) (long), but this type in C + + is illegal. 
  C + + does not provide similar __ thiscall this keyword modified type, because this type is requested compiler encountered function operators and members of operators, such as a.AB (10);, to the left of the member operators address as the first function call parameters into account-and then再传function operators in the rest of the parameters are given.    That is against this type of function at the same time operators and members of this particular operator, the compiler to provide some information to generate the correct code, but not for modified figure (figure modified to meet the requirements of all the circumstances).    That type is modified for the figure, and this type of figures can not be modified, C + + does not provide similar __ thiscall keywords. 
  And like before, as ABC:: AB is a mapping of address, rather than a shift duty, so can ABC:: AB, but not ABC:: a;, which is offset value.    According type matching, it is easy to know that can also: 
  Void (ABC:: * p) (long) = ABC:: AB; or void (ABC:: * p) (long) = & ABC:: AB; 
  Then there: void (ABC:: ** pP) (long) = &p; (c. ** pP) (10.0f).    The reason is because the function and operation of brackets at the top level than "*" high.    Thinking back to the former article said pointer type of conversion only type of change, numerical unchanged (the next numerical changing circumstances), it can have the following code, the code is meaningless in this function only deepen the understanding of the members . 
  (Long a struct ABC; void AB (long);); 
  Void ABC:: AB (long d) 
  ( 
  This-> 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) (0XABCD, 0XEF12); 
  ) 
  After the implementation of the above, the ca X00270004 for 0, 0 X0000EF12 ba, da XABCD 0, 0 db XFFFF.    C for the function call, AB:: ABC mapping address was direct conversion type then be used directly, it will be Skip to the procedures AB:: = ABC's a short (tem / 10); started, and parameter mapping tem is the transmission parameters of the first memory address, date and type of use long been explained tem 43, and then implementation.    Short attention b = (tem - tem / 10); reality is this-> b = short (tem - tem / 10), and this value is c corresponding address, but here are considered type AB * (because in the function AB:: ABC's function in vivo), it can be this-> b normal (ABC structure of the member variables not b), and b offset 2, the implementation of the sentence after the results of the 39 storage to c Address correspondence and two memory, and a short explanation of the type received 16 of the binary storage.    For a short = (tem / 10), nor do the same thing, so the final value of ca 0 X0027004 (39 decimal to hexadecimal 0-X27). 
  Similarly, b call, procedures will be Skip to the AB:: ABCD, but the call b generated code, it will be in accordance with the parameters 0 XABCDEF12 parameters for the types of records in the format of long transmission parameters of memory, and then Skip to the AB:: ABCD .    But compiler AB:: ABCD when short accordance with the parameters for the two types of mapping parameters to the corresponding tem1 and tem2 address, therefore easy to think of the value of tem1 will 0 XEF12, tem2 XABCD the value 0, but the actual is not the case.    How to transfer parameters before that by the rules of the function call, the function call in the details of a concrete realization of the "C + + from scratch (15)," that here that only members of the mapping function is still address, the type of decision and it how to use it, and behind that. 

  The meaning of the statement 

  As already explained what is the meaning of the statement in this function because of the definition of the rules of grammar of this new definition, we must reconsider the meaning of the statement.    Attention to the fact that, in front of the definition of a function will be put in front of the main function definition will be a statement that no longer function; Likewise, if the definition of a variable, not the variable that statement again.    This statement is a statement that is the definition of the function, but the function of the definition of the above statements is not the function of the statement, the following statement to understand the real meaning. 
  Statement asking the compiler to generate mapping elements statements.    The so-called mapping element is introduced in front of the variables and functions are only three columns (or three field): Type column, column name and address bar (members of variable types of this column take on the offset value).    Whenever see that the compiler declaration statements, generate a map element, and the corresponding address bar will be empty, and then left some information to tell connector - this. Obj files (source files compile the documents generated after , the VC yes. obj) needs some symbols, these symbols will be found later this revised and improved. obj file, the final connection. 
  I recall that prior to the meaning of the symbol, it is a string for the compiler and linker between the communication.    No type of symbol, because connector is responsible for search and perfect symbol (because some elements of mapping the address bar or empty) Intermediate document (for the VC. Obj files), do not syntax analysis, they do not have any type. 
  Requirements definition is filled compiler front statement did not write the address bar.    That is a variable corresponding addresses, only known when, in its definition.    Therefore, the actual distribution in the stack memory, and so the work was done by the completion of the definition of variables, the variables do not have a statement allocate memory.    But it should be a focus of attention, the definition is generated mapping elements need to address, it means that its definition of the generation which is the address mapping elements, and at this time the mapping table compiler (ie, before that the internal compiler used to record Table elemental mapping of variables, functions, etc.) there is no mapping element that is yet no corresponding elements of the statement appeared, then the compiler will be the error. 
  But only write in front of a variable or function definition language, it still did not normal error ah?    Actually very simple and need to be defined as a statement and a statement is only to the compiler of the information provided may vary.    Such as: void ABC (float); and void ABC (float) (), the same compiler on their view.    The former is a function of the type and types, the compiler just fill in the name of elemental mapping and type two columns.    Since then only to a ";", did not give the mapping function of the code, the compiler could not fill in the address bar.    As for the latter, given the function name, as well as mapping of the type code (the empty compound sentence), the compiler has to be filled out all the information which will be three columns of information to fill in the results statement on the definition of a complete performance a statement of function. 
  The variables, such as long a;.    Ditto, here are the names and types, the compiler type and filled out the names of two columns.    However, the corresponding variables on the stack is a piece of memory in the first address, the first code on the address can not be demonstrated by the (previous function by function on the back of the statement to express the written statements of the corresponding function of the corresponding code addresses), and must be passed by the compiler internal calculated, it was mandatory for writing counted as above as the definition of variables, but the variables in a statement on the need to increase extern front.    That is, as above will lead to an internal compiler then calculated that the corresponding addresses and filled out all the information mapping elements. 
  Above appears inevitable deliberately mystifying, it is because since the emergence of type definition.    Consider the definition of variables, such as: 
  Struct ABC (long a, b; double c;); 
  Given the above types - long ABC::, long ABC:: and double ABC::; given a name - ABC:: a, ABC:: b and ABC:: c; given address (that is, partial Shift) - 0,4 and 8, because it is self-defined structure type, it can be drawn from this statement to the members of the offset variable.    Above that three messages that can be filled out all the information mapping elements, can be counted as defined above statements.    For members of function, as follows: 
  Struct ABC (void AB (float);); 
  Given the above types - void (ABC::) (float); given a name - ABC:: AB.    However, because no address given, is not complete mapping of the elements of all the information above is a member function is ABC:: AB statement.    In accordance with the previous statement, as long as we can address given, and whether they need to or is the definition of the statement and, therefore, you can: 
  Struct ABC (void AB (float) ()); 
  Above types and names are given at the same time, gives the address, so it will be completely filled out all the information element mapping and definition.    Usage of the above has its particularity, and behind that.    Note that if further behind when ABC wrote:: AB definition statements, the following error: 
  Struct ABC (void AB (float) ()); 
  Void ABC:: AB (float) () 
  Will be above error, and the reason is very simple, because the latter is defined, it only provided ABC:: AB corresponding address this information, but the address mapping elements of the column has been filled out, the compiler will repeat that definition.    Individual members to look at the definition of function, it is the type of void (ABC::) (float), a name given ABC:: AB, it is also given address, but why is it only the address of this information ?    First, the name ABC:: AB is not in line with the rules of identifier, and type Xiuchifu ABC:: type definition must be at "()" to be able to add, have already repeatedly been stated.    Therefore the information given above is: given an address, the address is for the type of void (ABC::) (float), the name for ABC:: AB mapping element addresses.    View compiler on the results of this mapping elements, and if so, then fill in the corresponding address bar, or error, that is, only to write a void ABC:: AB (float) () is wrong, in their previous definition must first type Fu "()," the statement corresponding mapping elements.    This is said in front of the definition of just filled address bar does not generate mapping elements. 

  The role of the statement 

  It is clear definition of the role of a meaningful mapping (the name and address) that is to do, but the statement is what is the use?    It is the type of name generation, we must be why the type of name?    It only tells the compiler not to send the wrong that variable or function undefined?    Anything of the significance of their existence, Take a look at this code. 
  Extern "C" long ABC (long a, long b); 
  Void main () (long c = ABC (10, 20);) 
  A.cpp assumptions in the above code in writing, the compiler generated documentation a.obj, no problem.    But according to the note before connecting will be wrong, because they can not get symbols _ ABC.    ABC because of the name _ the corresponding address bar also empty.    Then in VC for a.cpp which works to add a new source file b.cpp, as writing code. 
  Extern "C" float ABC (float a) (return a;) 
  Compile and link, not any problem, but I believe that you have a problem - ABC function and the definition of the type of statement does not match, but a successful connection? 
  Note that the above statement on the link, no link types, just symbols.    Above using extern "C" makes demands _ ABC a.obj symbols, and provide b.cpp _ ABC's symbol, the only remaining b.obj connector will be in the corresponding address _ ABC put to perfect a a.obj . obj, and the final link a.obj b.obj. 
  Well above what the outcome because of the need to consider the function of the realization of the details, in the "C + + from scratch (15)," another note, and noted that as long as one thing here: the compiler even in the absence of addresses can still generate code to achieve function operator functions - function call.    This was because when the statement must be given a certain type and name, because that type of compiler, when an operator involved a mapping element, how to generate code to achieve this operator functions.    In other words, two char types of digital multiplication and two types of digital multiplication long compiler code generation different from the long ABC (long), the function call code and void ABC (float) different.    Namely, the number of operators role will lead to the type of compiler code generation different. 
  So why should the above definition of ABC put b.cpp?    Because the source document between the compiler is an independent, if on a.cpp, the compiler will find a mapping has been so elemental, but not type matching, error.    And put b.cpp, allowing the connector to improve a.obj, when there will be no kind of, just symbols.    Below continue. 
  Struct ABC (long a, b; void AB (long tem1, long tem2); void ABCD ();); 
  Void main () (ABC a; a.AB (10, 20);) 
  From the above that, although this did not give ABC:: AB definition, but still compiling successful, there is no problem.    Still a.cpp assumptions in the above code, and then add b.cpp in which to write the following code. 
  Struct ABC (float b, a; void AB (long tem1, long tem2); long ABCD (float);); 
  Void ABC:: AB (long tem1, long tem2) (a = tem1; b = tem2;) 
  Here definition of the function ABC:: AB, attention as previously mentioned, as a function of this definition merely definition, it is necessary to write in their previous type definition at "()" to allow the compiler to generate mapping elements.    But more should be noted here will be the location for the members of variables, such mapping is on the b 0 and 4 is a map, and also a, b replaced by the type of float, and even in the definition of widely divergent a.cpp .    But there is no problem, the compiler successful connection, a.AB (10,20); implementation 0 X41A00000 after aa, ab 0 X41200000, and * (float *) & a.a 20, * (flaot *) & a.b 10 . 
  Why?    Because only in the current compilers that compile the source files in the following types match, and another source files compiled, the compiler other source files generated by the mapping of all elements null and void.    Therefore statement will be bundled with the type and name, the name of their association on behalf of the types of address types of digital, and subsequent operation of the code in all of this figure operators compiler Health Chengdu this figure will be subject to the type of impact.    That the statement is to tell the compiler to generate code, not just a grammatical function of the variables or phrase, it is indispensable. 
  It should also be noted in the above two documents ABC:: ABCD members of the statement function different, and the whole project (ie a.cpp and b.cpp) are not ABC:: the definition of ABCD, but the compiler can connect successfully, statement is not because the compiler has been told what, but how to produce the code. 

  Header files 

  As already stated that if there is a custom type ABC, in a.cpp, b.cpp c.cpp and we must use it, it must be in a.cpp, b.cpp and c.cpp, prior to their use ABC Fu with type definition "()" redefined the definition of this type again.    If not careful, as in the above example and b.cpp a.cpp wrote the definition not the same, will be difficult to find a mistake.    To this end, C + + provides a pre-compiler directives to help. 
  Pre-compiler directive is compiled before the implementation of the directive by the pre-compiler it to explain the implementation.    Pre-compiler is another procedure, in most cases, the compiler vendors will be combined into a C + + compiler which only offer one.    In this note in the pre-compiler directive contains instructions - # include its format # include <file name>.    It should be noted pre-compiler directive must be separately accounted for and his entourage, and <file name> is a double quotation marks or angle brackets enclose the file name, such as: # include "abc.c" # include "C: \ abc.dsw" or # include <C:\abc.exe>.    Its role is very simple, that is, or the angle brackets, quotes a document written in a document to the corresponding ANSI format or MBCS format (on the two formats can refer to the "C + + from scratch (e)") explained, and the contents intact Fixed replacement to the location of # include, for example, abc Below is the content of the paper. 
  Struct ABC (long a, b; void AB (long tem1, long tem2);); 
  , In front of a.cpp can be changed to: 
  # Include "abc" 
  Void main () (ABC a; a.AB (10, 20);) 
  B.cpp and can be changed to: 
  # Include "abc" 
  Void ABC:: AB (long tem1, long tem2) (a = tem1; b = tem2;) 
  Then there would not have been similar, as in the above-defined types b.cpp ABC will be the definition of wrong and lead to erroneous results (aa X41A00000 for 0, 0 X41200000 ab), which a.AB (10, 20); implementation , a 10 aa, ab 20. 
  Note that the use of double quotes to include a living document, which said that when the omnibus is a living document or a relative path and did not give full path, as in the above abc, this search was first compiled source files the directory, and search for self-compiler contains the directory (eg: C: \ Program Files \ Microsoft Visual Studio. NET 2003 \ Vc7 \ include, etc.), there are generally placed compiler onboard the SDK header files ( on the SDK, will be in the "C + + from scratch (18)," note), if still not found, the error (Note that the compiler in general has been providing some options to enable Apart from the above list, but also can be Search the directory specified, the compiler different set different ways, not in the table). 
  If using angle brackets enclose, said that since the first compiler will search the directory contains further source files directory.    Why?    In order to prevent themselves from only the file name and the compiler is the directory containing the documents Chongming and conflict, because once found documents will no longer follow-up search directory. 
  Therefore, the general C + + code, if the use of a custom type, will be that type of self-definition of the respective definitions installed in the two documents, the above structure for ABC, it should generate two documents, namely the ABC. h and ABC.cpp, which ABC.h known as the header files, and ABC.cpp be called the source document.    First release of the document is a statement, and put the source file is defined, and on the content of ABC.h front of abc, and ABC.cpp b.cpp and on the content of the same.    Then whenever a project source files to be used in the ABC structure, in the beginning of that source document contains ABC.h, and this will be equivalent to all the relevant structures ABC statement that the documents are brought into the compiler, for example, in front of a.cpp through in the beginning include abc structure ABC in a statement. 
  Why should we create a ABC.cpp?    If ABC:: AB statement also put the definition in ABC.h, a.cpp to use ABC, and ABC c.cpp to use, so a.cpp contains ABC.h, inside ABC:: AB definition , and generates a symbol? ABC AB @ @ @ @ QAEXJJ Z (for the VC); c.cpp the same compiler to generate this symbol, and then connect, due to two of the same symbols, connectors, which can not be determined using one reported wrong.    Therefore definition of a specialized ABC.cpp will function ABC:: AB ABC.obj put in the definition, which would generate only a symbol, the connection will no longer error. 
  Note that the above struct ABC (void AB (float) ());.    If the on ABC.h, because in the type definition has been at the function on ABC:: AB is the definition, it would be ditto, a two identical symbols, then connect failed.    To avoid this problem, C + + types of provisions in the above definition of writing directly at the function definition is the definition of function inline function, for length, next introduced. 

  Members of the significance 

  Above that from the perspective of the grammar function of the meaning, if very faint, it does not matter, does not mean that the realization will not be able to understand the use, and the programmer is important that the language and the ability to apply knowledge of language is not (although the latter also important).    Below that members of the semantics. 
  This paper presents a beginning of a semantic - a resource with the function, and C + + together with the definition of types of operators "." And "->" the use of code from the very easy to demonstrate a kind of semantic - affiliation.    Such as: ab, cd respectively, said a b and c of the d.    Some resources to the function mapped to C + +, such resources should be mapped into a custom type, and it is the function of the mapping into this type of self-definition of function, such as the most mentioned at the beginning of the monster and play home, as follows: 
  Struct Player (float Life; float Attack; float Defend;); 
  (Struct Monster float Life; float Attack; float Defend; void AttackPlayer (Player & pla);); 
  Player player; Monster a; a.AttackPlayer (player); 
  Above semantic very obvious that the operation of the implementation of the code is a monster offensive player player, and player.Life player on behalf of the lives of players.    Monster write the following assumptions:: AttackPlayer definition: 
  Void Monster:: AttackPlayer (Player & pla) 
  ( 
  Pla.Life -= Attack - pla.Defend; 
  ) 
  Semantic above is clear: a monster offensive players is by means of attacking players will be the life of their own value minus the attacks of the audience to be attacked by the defense of values.    Semantic very clear, good readability of the code.    And if the original wording: 
  Void MonsterAttackPlayer (Monster & mon, Player & pla) 
  ( 
  Pla.Life -= mon.Attack - pla.Defend; 
  ) 
  The performance of the semantic code: attack player is a monster operation, the operation of the two operational needs resources, respectively monster types and types of players.    This semantic never originally intended to demonstrate the performance of our ideas, but the monster attacks functional Another explanation (on this point, in the "C + + from scratch (12)," elaborated), or more suitable for performance silver work.    For example checkout achieve is the work of money, customers at the counter to buy things from the salesperson out documents, and then get checkout, they realize that the documents.    Here checkout operators need to work on the two resources - money and documents, then they should be paid for this work mapping the function rather than the above members of function, because the algorithm, has not been mapped into checkout Since the definition of types of necessity, that we work on the cash register and who do not care, care only about how to do it. 
  At this point on the end of the half-defined types, through these elements can be prepared to reflect more complicated semantic code, the next will indicate the type definition of the latter half of the content, they can think of is fundamental to the needs of semantics, The following article is how will the remaining contents of semantics to explain, but each is still to explain how to achieve. 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Releated Articles

  • Popuklar Articles

0 Comments to “C + + from scratch (10) - what category”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.