C++链接性详解:外边链接与内部链接的区别与应用实例
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/5775f7058aa44a82b95b2952040fe927~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1722992224&x-signature=QZq2db5RjbcLjppxCOKoHrMtVtE%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">概述:</span></strong><span style="color: black;">在C++中,<span style="color: black;">外边</span>链接与内部链接是关于变量和函数<span style="color: black;">功效</span>域与可见性的重要概念。<span style="color: black;">外边</span>链接<span style="color: black;">准许</span>在<span style="color: black;">区别</span>文件中共享,而内部链接限制在当前文件内<span style="color: black;">拜访</span>。<span style="color: black;">经过</span>实例源代码,清晰展示了它们的用法,<span style="color: black;">帮忙</span>理解<span style="color: black;">功效</span>域与可见性的<span style="color: black;">区别</span><span style="color: black;">需要</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在C++中,<span style="color: black;">外边</span>链接(external linkage)和内部链接(internal linkage)是与变量、函数以及常量的<span style="color: black;">功效</span>域和可见性<span style="color: black;">关联</span>的两个重要概念。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">1. <span style="color: black;">外边</span>链接</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">外边</span>链接<span style="color: black;">暗示</span>变量、函数或常量<span style="color: black;">能够</span>被其他文件<span style="color: black;">拜访</span>。关键字 </span><span style="color: black;">extern</span> 常用于声明<span style="color: black;">拥有</span><span style="color: black;">外边</span>链接的变量。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">实例源代码:</h1><span style="color: black;">// File1.cpp</span>
<span style="color: black;">#<span style="color: black;">include</span> <span style="color: black;"><iostream></span></span>
<span style="color: black;">// 声明<span style="color: black;">拥有</span><span style="color: black;">外边</span>链接的全局变量</span>
<span style="color: black;">extern</span> <span style="color: black;">int</span> globalVariable;
<span style="color: black;"><span style="color: black;">int</span> <span style="color: black;">main</span><span style="color: black;">()</span> </span>{
<span style="color: black;">std</span>::<span style="color: black;">cout</span> << <span style="color: black;">"File1: "</span> << globalVariable << <span style="color: black;">std</span>::<span style="color: black;">endl</span>;
<span style="color: black;">return</span> <span style="color: black;">0</span>;
}
<span style="color: black;">// File2.cpp</span>
<span style="color: black;">#<span style="color: black;">include</span> <span style="color: black;"><iostream></span></span>
<span style="color: black;">// 定义<span style="color: black;">拥有</span><span style="color: black;">外边</span>链接的全局变量</span>
<span style="color: black;">int</span>globalVariable =<span style="color: black;">42</span>;
<span style="color: black;"><span style="color: black;">int</span> <span style="color: black;">main</span><span style="color: black;">()</span> </span>{
<span style="color: black;">std</span>::<span style="color: black;">cout</span> << <span style="color: black;">"File2: "</span> << globalVariable << <span style="color: black;">std</span>::<span style="color: black;">endl</span>;
<span style="color: black;">return</span> <span style="color: black;">0</span>;
}
<h1 style="color: black; text-align: left; margin-bottom: 10px;">2. 内部链接</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">内部链接<span style="color: black;">暗示</span>变量、函数或常量只能在当前文件内部<span style="color: black;">拜访</span>。<span style="color: black;">运用</span>关键字 </span><span style="color: black;">static</span> <span style="color: black;">能够</span>使变量、函数或常量<span style="color: black;">拥有</span>内部链接。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">实例源代码:</h1><span style="color: black;">// File1.cpp</span>
<span style="color: black;">#<span style="color: black;">include</span> <span style="color: black;"><iostream></span></span>
<span style="color: black;">// 声明<span style="color: black;">拥有</span>内部链接的全局变量</span>
<span style="color: black;">static</span> <span style="color: black;">int</span> internalVariable = <span style="color: black;">10</span>;
<span style="color: black;"><span style="color: black;">int</span> <span style="color: black;">main</span><span style="color: black;">()</span> </span>{
<span style="color: black;">std</span>::<span style="color: black;">cout</span> << <span style="color: black;">"File1: "</span> << internalVariable << <span style="color: black;">std</span>::<span style="color: black;">endl</span>;
<span style="color: black;">return</span> <span style="color: black;">0</span>;
}<span style="color: black;">// File2.cpp</span>
<span style="color: black;">#<span style="color: black;">include</span> <span style="color: black;"><iostream></span></span>
<span style="color: black;">// 错误:<span style="color: black;">没法</span><span style="color: black;">拜访</span><span style="color: black;">拥有</span>内部链接的变量</span>
<span style="color: black;">// std::cout << "File2: " << internalVariable << std::endl;</span>
<span style="color: black;"><span style="color: black;">int</span> <span style="color: black;">main</span><span style="color: black;">()</span> </span>{
<span style="color: black;">return</span> <span style="color: black;">0</span>;
}
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">外边</span>链接<span style="color: black;">准许</span>变量、函数或常量在多个文件之间共享,而内部链接限制了它们的可见性,仅在当前文件内可<span style="color: black;">拜访</span>。<span style="color: black;">选取</span><span style="color: black;">运用</span><span style="color: black;">外边</span>链接还是内部链接取决于变量或函数的<span style="color: black;">功效</span>域<span style="color: black;">需要</span>。</span></p>
感谢你的精彩评论,带给我新的思考角度。 楼主的文章深得我心,表示由衷的感谢! 你说得对,我们一起加油,未来可期。 请问、你好、求解、谁知道等。
页:
[1]