Description of extern in C ++ (VC code)

Source: Internet
Author: User

Extern;

 

It is used to tell the compiler that this variable is defined in other files (foreign aid ).
Never be surprised to see its name.

 

The compiler believes in conferences. Therefore, if a variable is viewed during compilation, it will be regarded as exist and no error will be reported. Only
When the link is connected, the linker will go to other OBJ files to find the definition (address) of the variable. If the link is found, the link goes smoothly. Otherwise, an error is returned.

Because the compiler only needs to know the name of the variable declared by extern, extern int A can be written as extern A (that is, the variable type is omitted ).

Not all variables can be declared with extern. Only variables with global variables and without static declarations can be declared as extern. also, do not place the extern. in the H file, you feel that the program is not wrong, but there is a link error. extern must be placed in the CPP file,

The H file is only responsible for declaring, and does not do any other work. Do not assign too many features to the H file, so that you do not know how the error occurs.

The test procedure is as follows:

Create two classes test1 and circle in VC ++, and define the variable OK in test1.h.

Declare the extern OK variable in the CPP of circle. They are as follows:

Test1.h

 

# If! Defined (afx_test1_hsf-c6342063_3cf3_48c3_940b_45e87d938f5d1_included _) <br/> # define tuning _ </P> <p> # If _ msc_ver> 1000 <br/> # pragma once <br/> # endif/_ msc_ver> 1000 </P> <p> int okay = 30560; <br/> class test1 <br/>{</P> <p> Public: <br/> test1 (); <br/> virtual ~ Test1 (); <br/>}; </P> <p> # endif

 

Test1.cpp

# Include "test1.h" </P> <p> test1: test1 () <br/>{</P> <p >}</P> <p> test1: :~ Test1 () <br/>{</P> <p >}< br/>

 

Circle. h

# If! Defined (afx_circle_h00009a50c655_f7db_4299_ae5f_8e42caf773ad00000000ded _) <br/> # define tuning _ </P> <p> # If _ msc_ver> 1000 <br/> # pragma once <br/> # endif </P> <p> class circle <br/>{< br/> public: <br/> circle (); <br/> virtual ~ Circle (); <br/> void fun (); <br/>}; </P> <p> # endif <br/>

 

Circle. cpp

 

# Include "circle. H "<br/> # include" iostream. H "</P> <p> extern OK; <br/> circle: Circle () <br/>{</P> <p >}</P> <p> circle ::~ Circle () <br/>{</P> <p >}< br/> void circle: Fun () <br/>{< br/> cout <OK <Endl; <br/>}< br/> int main () <br/>{< br/> circle * CI = new circle (); <br/> Ci-> fun (); <br/> cout <"commandid 0000²» export C ++/N"; <br/> return 0; <br/>}

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.