A project created using MFC is composed of many files. It cannot define global variables as in General C ++ programs, to define global variables and functions that can be shared by multiple files in the project, you must use some special methods. There are actually many ways to implement it. Here we only introduce two methods.
1. Define in the application class
In the project generated using MFC, there is a class named cxxxapp, which is derived from the cwinapp class. This class mainly initializes the program and generates documents and view objects. We can define the variables and functions that require global access as member variables and member functions of this class to implement global access.
Strictly speaking, such variables and functions are not global, because they are still only members of the class, just because we can easily obtain pointers to the cxxxapp class, therefore, you can access them in documents, views, dialogs, and various custom classes to achieve the same effect as global variables. Use the function "afxgetapp ()" to obtain the cxxxapp class pointer and use "afxgetapp ()-> member" to access the variable or function.
Example:
Test. h: (Application Class header files) Class ctestapp: Public cwinapp { Public: Int X; // global variable Int F (INT y); // global function ............ }; |
Test. cpp: (application program files) Int ctestapp: F (INT y) // global Function Definition { Y ++; Return y; } |
Variables and functions defined in the ctestapp class can be accessed in other classes. For example, to access the variables X and F () in a function of a view ():
Void ctestview: XYZ () { Ctestapp * APP = (ctestapp *) afxgetapp (); // generates a pointer to the application class App-> x = 0; // access variable X Int z = app-> F (1); // access function f () ............ } |
In this way, the variables X and function f () can be regarded as global.
The global variables and global functions implemented in this method are relatively simple, but they also have disadvantages. First, they are not easy to access and need to obtain the pointer of the application class each time; then, we put some variables and functions irrelevant to the application class itself in it, which makes the class look strange and destroys the class encapsulation.
2. Use static variables and static functions
I like the calling method of API functions. No matter which class I use, I can call the method by using ": API function. Reasonable Use of static can achieve similar global variables and global functions.
Static variables and static functions have the following properties:
If a data member is declared using the static keyword in a class, only one copy exists for this data member. No matter how many instances are created for this class, only one copy exists, even if no instance of this class is created, it also exists.
If a function is declared using the static keyword in a class, the function can be accessed using the "Class Name: function name" method, without reference to the instance of this class, or even the instance of this class can not exist.
It is convenient to use global variables and functions implemented using this property.
It is worth noting that it is best to encapsulate global variables and global functions in a centralized manner. do not define them in documents, views, and other classes so that they can be used globally.
Example:
1. Add a new class without a base class and set the class name to cpublic.
Click the "New Class" command under the "insert" menu, select "class type" as "generic class", and enter the class name "cpublic" in the "name" column ", click OK. The new class is created.
2. Include the header file of the public class so that each class can access it
The header file of cpublic should be included in the header file of the application class, so that the cpublic class does not need to be included when it is referenced in other classes.
Test. h: (Application Class header files) # Include "public. H" // contains the public Class header file Class ctestapp: Public cwinapp { ............ }; |
3. global variables and global functions are defined in the public class and modified using static. Static variables must also be defined and initialized outside the class.
Public. h: (Public Class header files) Class cpublic { Public: Cpublic (); Virtual ~ Cpublic (); Public: Static int X; // global variable Static int time; // global variable Static int F (INT y); // global function ............ } |
Initialize and define the static variables in the public class:
Public. cpp :( public program file) Int cpublic: x = 0; // initialize the global variable Int cpublic: time; // defines the global variable Cpublic: cpublic () { } Cpublic ::~ Cpublic () { } Int cpublic: F (INT y) // global function. Do not add static { Y ++; Return y; } |
4. Global usage
Use the variable cpublic: variable name
Use Function: cpublic: function ()
For example, access the variables X and F () in a function in the View ():
Void ctestview: XYZ () { Cpublic: x = 0; // access variable X Cpublic: time = cpublic: F (1); // access function f () ............ } |
The methods for accessing X, time, and F () in other classes are the same.
5. Notes:
① Because the static volume can exist independently of the class, instances of the cpublic class do not need to be generated.
② The definition and initialization of static data members must be performed outside the class, such as the initialization of X in the example. Although the variable time is not initialized, it must also be defined outside the class. Because no cpublic class instance is generated, its constructor and destructor are not executed, and it makes no sense to do anything in it.
③ If a static function needs to access the variables in the cpublic class, these variables must also be static. Because the non-static volume does not exist when the instance is not generated.
For example:
Class cpublic { Public: Int X; // internal variable Static int F (INT y) // global function { X ++; Return X; }; ............ }; |
Although X is a member of the class, if you do not generate an instance of the cpublic class, the F () function will exist, and the variable X does not exist.
In short, managing the global volume of classes without instances is a good choice. It has the advantages of centralized management and ease of use. Of course, unless necessary, it is better to reduce the global volume. A good programmer will never abuse the global volume at will, and a program that is encapsulated and not done well, modification and maintenance will make you suffer.