Summary 2.extern keyword

2.1 Definitions and declarations, initialization and assignment

(1) Definition: entity responsible for creating the name associated with it.

// (current file) declare variables a, defined and A 
int A = . 5 ;

(2) Statement: make known the name of the program, defined elsewhere using the name must contain a statement (such as defining other files)

// declare a, not the definition of 
extern  int A;

(3) a statement given an initial value

// definition of A 
extern  int A = . 5 ;

However, there are exceptions

1  // error, do not allow such a function in the interior of 
2  void the Test () {
 . 3      // definition of A 
. 4      extern  int A = . 5 ;
 . 5 }

(4) Initialization: scratch process, the space allocated first, then fill transactions;
       assignment: the operation of the existing object.

1  // be initialized to a. 5 
2  int a = . 5 ;
 . 3  // be assigned to a. 6 
. 4 a = . 6 ;

2.2.extern a variety of uses

(1) extern: When can be placed before the variable or function to define variables or functions indicated in other documents, suggesting that the compiler encounters this variable and function to find its definition in another module. In addition extern also be used to specify the link.

(2) extern variable modification

A common mistake

// There follows in test1.cpp 
int Array [ . 6 ];
 // There follows in test2.cpp 
extern * Array;

Array [] is a pointer to an array of type int, test2.cpp the statement int type pointer pointing to large difference between the two.

A common application

       extern use often have such a role in the variable declaration, you declare a global variable in the * .cpp file, if you want this global variable is referenced, it is placed in * .h and declared with extern

(3) extern modified function

A problem when used to declare the function:

       If the function provided unilaterally modify the function prototype, if the parameter is missing or changed the members of some internal functions operational, the system will lead to error, but the compiler was able to pass. One solution allows providers to provide the function declarations in the header file, and include the header file.

When the function is defined as a part of:

       If the function declaration with the keyword extern, merely suggesting that this function may be defined in another source file, no other action.

When declaring global variables when put together with the definition:

       因为#include的存在而产生重复定义的链接错误。所以:只在头文件中做声明,真理就是这么简单。当然不使用#include语句,将想要提供给外部接口的函数和变量全部使用extern来修饰也是一种方法。你用么,反正我不用。

(4)extern "C" 

在C++环境下使用C函数的时候,常常会出现编译器无法找到obj模块中的C函数定义,从而导致链接失败的情况,应该如何解决这种情况呢?

        C++语言在编译的时候为了解决函数的多态问题,会将函数名和参数联合起来生成一个中间的函数名称,而C语言则不会,因此会造成链接时找不到对应函数的情况,此时C函数就需要用extern “C”进行链接指定,这告诉编译器,请保持我的名称,不要给我生成用于链接的中间函数名。

使用时注意两点:

       指向extern "C"函数的指针:

1 void(*Test)(int);
2 extern "C" void(*Test_1)(int);
3 //错误,Test和Test_1的类型不同
4 Test = Test_1;

       重载函数与链接指示:

C语言没有重载函数,有了一组C语言函数之后,其同名函数必须为C++函数,否则出错。

(5)extern 与 static 

(6)extern 与 const

Guess you like

Origin www.cnblogs.com/Royzzzzz/p/10959630.html