Define global variables in MFC

Source: Internet
Author: User

Global variables are generally defined as follows:
1. Define int Myint in Class 1. cpp;
Then, use extern int Myint in. cpp where you want to use it.

2. Add the following to stdafx. cpp:
Int Myint;
Then add the following to stdafx. h:
Extern int Myint
Variables defined in this way will be visible in any file.

If it is a Class Object:
In stdafx. H, the statement is as follows:
Extern cmyclass g_class;
In stdafx. cpp, define:
Cmyclass g_class;
But there is one thing: extern cmyclass g_class in stdafx. h; the definition of cmyclass or # include cmyclass must be included before this sentence.

3. It is more standardized to define a glbs. h and put the original definitions of all global variables in it. Define an externs. h and add extern to all the variables you previously defined in glbs. h. Note: If you set an initial value in glbs. H, do not add a value to externs. h. The # I nclude <glbs. h> that is called for the first time and the # I nclude <externs. h> that is called later

4. Define variables in the CAPP:
Cstring g_suser; // defines the global
In the class to be referenced:
Extern csvapp theapp; // Add this sentence before the. cpp file constructor.
When referencing: If (theapp. m_susername = ""); // reference it like this.

5. To use a custom global custom class, you can use the following methods in all files.
1. Add the Class header file to stdafx. h.
2. Add the class definition to stdafx. cpp, for example, cmyclass g_class;
3. In the app class of your program, if the header file of the ctestprojectapp class is prefixed with extern cmyclass g_clas

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.