b1gc8v 发表于 2024-8-4 09:37:45

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&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723338468&amp;x-signature=Z26WRMbUBZ%2FF4k5Y402zAFD%2Fd6g%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></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;">&lt;iostream&gt;</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> &lt;&lt; <span style="color: black;">"File1: "</span> &lt;&lt; globalVariable &lt;&lt; <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;">&lt;iostream&gt;</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> &lt;&lt; <span style="color: black;">"File2: "</span> &lt;&lt; globalVariable &lt;&lt; <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;">&lt;iostream&gt;</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> &lt;&lt; <span style="color: black;">"File1: "</span> &lt;&lt; internalVariable &lt;&lt; <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;">&lt;iostream&gt;</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 &lt;&lt; "File2: " &lt;&lt; internalVariable &lt;&lt; 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></p>




听听海 发表于 2024-8-21 20:29:38

你的话语真是温暖如春,让我心生感激。

nqkk58 发表于 2024-9-29 17:02:16

i免费外链发布平台 http://www.fok120.com/

j8typz 发表于 2024-10-21 08:29:41

真情实感,其含义为认真了、走心了的意思,是如今的饭圈常用语。

nykek5i 发表于 2024-11-7 08:16:41

论坛的成果是显著的,但我们不能因为成绩而沾沾自喜。
页: [1]
查看完整版本: C++链接性详解:外边链接与内部链接的区别与应用实例