VC++的一个类中调用另一个类的变量

#i nclude "stdafx.h"
1.extern用在变量声明中常常有这样一个作用,你在*.c文件中声明了一个全局的变量,这个全局的变量如果要被引用,就放在*.h中并用extern来声明。
2.如果函数的声明中带有关键字extern,仅仅是暗示这个函数可能在别的源文件里定义,没有其它作用。即下述两个函数声明没有区别:
extern int f(); 和int f(); 
================================= 
如果定义函数的c/cpp文件在对应的头文件中声明了定义的函数,那么在其他c/cpp文件中要使用这些函数,只需要包含这个头文件即可。
如果你不想包含头文件,那么在c/cpp中声明该函数。一般来说,声明定义在本文件的函数不用“extern”,声明定义在其他文件中的函数用“extern”,这样在本文件中调用别的文件定义的函数就不用包含头文件
include “*.h”来声明函数,声明后直接使用即可。
=================================
举个例子:
//extern.cpp内容如下:

// extern.cpp : Defines the entry point for the console application.
//

#i nclude "stdafx.h"
extern print(char *p);
int main(int argc, char* argv[])
{
   char *p="hello world!";
   print(p);
   return 0;
}
//print.cpp内容如下
#i nclude "stdafx.h"
#i nclude "stdio.h"
print(char *s)
{
   printf("The string is %s\n",s);
}

结果程序可以正常运行,输出结果。如果把“extern”去掉,程序依然可以正常运行。

由此可见,“extern”在函数声明中可有可无,只是用来标志该函数在本文件中定义,还是在别的文件中定义。只要你函数在使用之前声明了,那么就可以不用包含头文件了。
原文地址:https://www.cnblogs.com/imhurley/p/2059294.html