⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_15416.htm

📁 一些关于vc的问答
💻 HTM
字号:
<p>
序号:15416 发表者:hope 发表日期:2002-09-18 10:55:13
<br>主题:VC中定义变量时用extern,有什么用处?怎么用法?
<br>内容:VC中定义变量时用extern,有什么用处?怎么用法?
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:天花乱追 回复日期:2002-09-18 11:00:37
<br>内容:好像是引用一个外部变量吧,具体你可查msdn<BR><BR>Static Storage-Class Specifiers<BR>The static storage-class specifiers, static and extern, can be applied to objects and functions. Table 6.1 shows where the keywords static and extern can and cannot be used.<BR><BR>Table 6.1&nbsp;&nbsp; Use of static and extern<BR><BR><BR>Construct Can static<BR>be Used? Can extern<BR>be Used? <BR>Function declarations within a block No Yes <BR>Formal arguments to a function No No <BR>Objects in a block Yes Yes <BR>Objects outside a block Yes Yes <BR>Functions Yes Yes <BR>Class member functions Yes No <BR>Class member data Yes No <BR>typedef names No No <BR><BR><BR>A name specified using the static keyword has internal linkage except for the static members of a class that have external linkage. That is, it is not visible outside the current translation unit. A name specified using the extern keyword has external linkage unless previously defined as having internal linkage. For more information about the visibility of names, see Scope and Program and Linkage in Chapter 2.<BR><BR>Note&nbsp;&nbsp; Functions that are declared as inline and that are not class member functions are given the same linkage characteristics as functions declared as static.<BR><BR>A class name whose declaration has not yet been encountered by the compiler can be used in an extern declaration. The name introduced with such a declaration cannot be used until the class declaration has been encountered.<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:雷神 回复日期:2002-09-18 11:24:18
<br>内容:这是C++的存储类说明符之一。用extern声明的变量,具有全局的生命周期。它定义变量在函数之外。所以称为外部变量。可以使用它声明一个变量,并在另一个源码调用。<BR>举例:<BR>#include &lt;iostream.h&gt;<BR><BR>extern int ival; <BR><BR>int main()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;ival++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;ival&lt;&lt;"\n";&nbsp;&nbsp;//输出11<BR>&nbsp;&nbsp;&nbsp;&nbsp;funa();<BR>&nbsp;&nbsp;&nbsp;&nbsp;return 0;<BR>}<BR>int ival=10;&nbsp;&nbsp; //可以在这定义IVAL的初值<BR><BR>void funa(void)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;ival++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;ival&lt;&lt;"\n"; //输出12<BR>&nbsp;&nbsp;&nbsp;&nbsp;funb();<BR><BR>}<BR><BR><BR><BR>___________________________<BR>// File B<BR>//<BR><BR>#include &lt;iostream.h&gt;<BR>extern int ival;&nbsp;&nbsp;//引用外部变量<BR>void funb(void)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;ival++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;("%d\n",ival); //输出13<BR>}
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:爱丽思 回复日期:2002-09-18 11:27:08
<br>内容:就是在不同.cpp文件里定义的全程变量
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:巴人 回复日期:2002-09-18 11:47:02
<br>内容:&nbsp;&nbsp;<BR>&nbsp;&nbsp; VC中定义变量时用extern,是一个变量声明语句,没有开辟空间,告诉编译器该变量是一个外部变量。<BR>&nbsp;&nbsp; <BR>用处:&nbsp;&nbsp; <BR>&nbsp;&nbsp; 项目中由多个源文件,当多个源文件需要使用同一个变量时,在一个源文件里定义一个变量,在另一个源文件里用extern修饰变量定义,表示引用一个外部变量。这样就可以在多个源文件中使用同一个变量,通过引用相同变量在不同的文件中传值。<BR>&nbsp;&nbsp; 在默认的情况下,没有加上extern修饰表示定义一个不同的变量,编译器会开辟变量的内存空间。<BR>用法:<BR>&nbsp;&nbsp; 在一个源文件中定义变量,其余的源文件中声明改变量(extern修饰变量定义)。<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:零 回复日期:2002-09-18 19:50:32
<br>内容:同意巴人<BR>还有,extern 不仅仅是文件间的,文件内也可以用。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -