1. Function of variables Function of Variables
Overall, variables are divided into two types of storage and service.
The former, such as cpeople * p = new cpeople (), which is characterized by one by one with objective objects, and the number is generally large, and should be the same as the objective object.
The latter, such as COODBDATABASE DB, which is characterized by completion of a function, the number can be completed (often one), and the survival period can be just the execution time of this function; but for convenience, avoid multiple definitions, often More than this cycle.
Therefore, if a class needs to be declared a large number of instances, do not declare the member variables used in the service (at least not to declare those big), use the following methods to call the service:
Declare a member variable of Static, allowing all instances to share a variable; the service required in the member function implementation, but if the service is a class that requires constructors and sectors, the speed is lowered; reference the pointer of the external service class;
Variables Can Be Divided Mainly Into Two Types: for Storage and for Service.
Sample for storage is CPeople * p = new CPeople (), its characteristic is that it is corresponding to a real object, and the amount of this type of variable maybe large, it exists as long as the real object need.
Sample for service is COODBDatabase Db, its characteristic is that it exists for only a function, the amount of this type of variable is decided by the function requirement and it lives only during the function; but for convenience and avoiding repeated definition, some can outlive .
SO if a class will be instanced for a large number,
1.2 Variable Form Life Area ?? of variables
Global variable
Most programs can do their functions without any global variables, so global variables should not be used. However, in the following, global variables are conveniently and safely:
1 This variable type only declares a unique instance and the survival period is the same as the process:
Main window or main process: such as extern cfhthostapp theapp; extern cfhthostdlg * g_pdlg; its naming does not need to be very specific; unique instance of this type: CMYCAHOST * G_PMYCAHOST; COODBDATABASE COODBDATABASE :: m_defaultoodbdatabase;
If you need to declare more than two instances, press the specification with g_ start, and the naming is specifically, with the difference.
2 Functions of non-VC classes, and need to be delivered with global variables with its specific parameters:
RPC implementation functions, such as:
Extern "C" boolean rpcsethost (handle_t id1, int * pnsize, unsigned char * szcahost) {... g_caconsole.sethost (PCAHOST); ...} thread function, such as:
DWORD WINAPI CRPCSERVER :: ServerThread (LPVOID PPARAM) {... g_pserver-> stopserver (); ...}
Note that the use of global functions can increase the stability of global variables.
CADPS system :: fHTLOADCONFIG () function actually called the global variable ccaregistry :: g_caregistry, use global function to block global variables, limit this global variable application in the global function, so only this class administrator guarantee The application in this function is secure, and the call to this function is also safe.
The benefits of using global function package global variables are similar to those of the class. The first principle that can be programmed in the "Design Mode" book is: Programming for the interface, not for implementation programming. This allows the software of the application layer to be completely not changed when the data structure needs to be changed.
The unnecessary global variable is converted to other variables to improve system stability.
Class member variable
The member variable of the class is used to store the address of the class data, state, external data, or service. The following is some applications with appropriate and improper members of the member variable:
1 Experience in the case of serving the member variables can be resolved by other methods, so that the member variable is only stored.
For example, CtyPTRARRAY
In fact, in the previous example, only one rather than a group of COODBField can do this, but because the program is more preferred than the efficiency, it is not aware of less affected (15 and 1 memory occupancy. Large) and does not consider extreme means of optimization efficiency.
If the data used for the service is small, it is not suitable for individual declarations of the class separately, and can be changed to a temporary variable in the service function. However, some data is constructed and destroyed quite spend time (such as CString / CDBVARIANT, etc.), this time you can consider declaring this data as a STATIC variable. related information
2 Most of the global variables other than the above use occasions can be replaced by a class member variable in an external service address.
In COODBQuery, there is a variable coodbcdmap * m_cpoodbcdmap. This data stores mapping information corresponding to such a column and variable addresses in the global variable coodbcdmaps g_oodbcdmaps. Since a variable is used to store the address of this mapping information, CoodbQuery calls m_cpoodbcdmap = ::fhtoodbcdmapof (aclass.cdmapname ()) to initialize this variable instead of using global variables directly. Thus, the global variable will no longer need to use the overall variable in this operation, but only need to judge the legality of m_cpoodbcdmap. Class function variable, global function variable
(slightly)
1.3 Variable Effects State of Variables
Static variable
Because only the same instance is used at runtime, it can be saved for the runtime operation time of its constructor and the destroyer to save time. Common structures and destroyers, slow classes include CString, Cole Series, etc. Special attention is to note that static variables are initialized such as static int ncount = 0; only once, so this application should pay special attention to clear the last parameter data or avoid it.
A common application of static variables is to record data at the last runtime, but from experience, if it is in a function of the class, it should be directly declared that the variables of this data can make the app more easy to understand and control.