Use of C ++ global variables

Source: Internet
Author: User

Yesterday I read the pen questions of Xintai technology.

The first feeling is that the foundation for the next session is too poor.

Then I found an interesting question: how to use C ++ global variables in other CPP files?

My first thought was to use it directly. However, I feel that it is better to add "include" and ":". By the way, I asked people who are sleeping in the same room. I found that the answer was completely different. He said that we had to add extern to every CPP, and we were stuck. So we started vs2005 and started programming verification. Then we found that if it is a CPP file, how to do it is correct. If there are two or more CPP files, the correct format is as follows:

OO. h

# Ifndef oo

# Define oo

Extern int A; // note that values cannot be assigned here.

# Endif

OO. cpp

# Include <iostream>

Using namespace STD;

Int A = 10; // defines the variable.

Void func () {A = 20; cout <;}

XX. cpp

# Include <iostream>

# Include "Oo. H"

Using namespace STD;

Int main ()

{

Cout <A; // here, a prints 10, which indicates that a = 10 in OO. cpp is executed first.

Func ();

}

This seems to be a correct practice. I don't know if there are other practices.

 

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.