C ++ global variables should be written as much as possible in stdafx. cpp. If the extern keyword cannot be found, the external definition is equal to the new definition.

Source: Internet
Author: User

I am writingProgramDuring the process, we encountered a strange problem of no initialization.

Finally, we found that a global variable A is written in the CPP where the main function is located. Due to the loading sequence of the header file, a problem occurs when a Class B is used, some data in a is expected to be obtained, but nothing is actually 0, as if a was just initialized.

 

I don't know what the root cause is. I only know that it must be extern.

In Class B, extern is used to define. However, because the header file of Class B is earlier than the global variable a defined by the main function in CPP, this extern does not seem to work, And vc6 does not prompt errors, as if it is declared directly by default.

 

The lessons learned are:

Global variables are covered in stdafx. cpp. Pay attention to the loading sequence of header files.

 

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.