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

📄 friendfunctionclass.html

📁 关于 C++ 的历史无须我来介绍了
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

  
  <link rel="stylesheet" href="css/stdlayout.css" type="text/css">

  
  <link rel="stylesheet" href="css/print.css" type="text/css">

  
  <meta content="text/html; charset=gb2312" http-equiv="content-type">

  
  <title>friend 函式、friend 类别</title>
</head>


<body>

<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>

<h1><a href="CppGossip.html">C++
Gossip:&nbsp;friend 函式、friend 类别</a></h1>

在定义类别成员时,私用成员只能被同一个类别定义的成员存取,不可以直接由外界进行存取,然而有些时候,您希望提供私用成员给某些外部函式来存取,这时您
可以设定类别的“好友”,只有好友才可以直接存取自家的私用成员。 <br>

<br>

下面这个程式中使用friend关键字来设定类别的好友函式,该好友可以直接存取该类别的私用成员: <br>

<ul>

  <li>Ball.h</li>

</ul>

<pre>class Ball;<br><br>int compare(Ball&amp;, Ball&amp;);<br><br>class Ball { <br>public: <br>    Ball(double, char*);     <br>    <br>    double radius() {<br>        return _radius;<br>    }<br>    <br>    char* name() {<br>        return _name; <br>    }<br>    <br>    void radius(double radius) {<br>         _radius = radius;<br>    } <br>    <br>    void name(char *name) {<br>         _name = name;<br>    }<br>    <br>    // 宣告朋友函式 <br>    friend int compare(Ball&amp;, Ball&amp;);<br>    <br>private:<br>    double _radius; // 半径 <br>    char *_name;  // 名称 <br>};</pre>

<br>

<ul>

  <li>Ball.cpp</li>

</ul>

<pre>#include "Ball.h"<br><br>// compare 为 Ball 的 friend <br>int compare(Ball &amp;b1, Ball &amp;b2) {<br>    // 可直接存取私用成员<br>    if(b1._radius == b2._radius)<br>        return 0;<br>    else if(b1._radius &gt; b2._radius)<br>        return 1;<br>    else<br>        return -1;<br>}<br><br>Ball::Ball(double radius, char *name) { <br>    _radius = radius; <br>    _name = name;<br>}<br></pre>

<br>

<ul>

  <li>main.cpp</li>

</ul>

<pre>#include &lt;iostream&gt;<br>#include "Ball.h"<br>using namespace std;<br><br>int main() {<br>    Ball b1(10, "RBall");<br>    Ball b2(20, "GBall");<br>    <br>    switch(compare(b1, b2)) {<br>        case 1:<br>             cout &lt;&lt; b1.name() &lt;&lt; " 较大" &lt;&lt; endl;<br>             break;<br>        case 0:<br>             cout &lt;&lt; b1.name() &lt;&lt; " 等于 " &lt;&lt; b2.name() &lt;&lt; endl;<br>             break;<br>        case -1:<br>             cout &lt;&lt; b2.name() &lt;&lt; " 较大" &lt;&lt; endl;<br>             break;<br>    }<br>        <br>    return 0;<br>}</pre>

<span class="postbody"><br>

执行结果:</span><br>

<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">

  <tbody>

    <tr>

      <td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">GBall 较大</span></small><span style="color: rgb(255, 255, 255);"><br>

      </span></td>

    </tr>

  
  </tbody>
</table>

<br>

使用friend函式通常是基于效率的考量,以直接存取私用成员而不透过函式呼叫的方式,来省去函式呼叫的负担,另外您也可以使用friend来重载
(Overload)运算子,之后的主题中会介绍。<br>

<br>

您也可以将某个类别宣告为friend类别,被宣告为friend的类别可以直接存取私用成员,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">class Ball;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int
compare(Ball&amp;, Ball&amp;);</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">class Ball { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">public: </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; ....</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;
// 宣告朋友类别</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;
friend class SomeClass;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">private:</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; ....</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">};</span><br>

</div>

<br>

如上宣告的话,SomeClass的实例就可以存取Ball实例的私用成员。<br>

<br>

</body>
</html>

⌨️ 快捷键说明

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