C++中友元的示例分析

這篇文章將為大家詳細(xì)講解有關(guān)C++中友元的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)是一家專注于做網(wǎng)站、成都網(wǎng)站制作與策劃設(shè)計(jì),扎蘭屯網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:扎蘭屯等地區(qū)。扎蘭屯做網(wǎng)站價(jià)格咨詢:18982081108

C++中友元的實(shí)例詳解

盡管友元被授予從外部訪問(wèn)類的私有部分的權(quán)限,但他們并不與面向?qū)ο蟮木幊趟枷胂嚆?;相反他提高了公共接口的靈活性。

一、友元類

友元聲明可以位于公有、私有活保護(hù)部分、其所在位置無(wú)關(guān)緊要

我直接貼出一個(gè)摘自< c++ primer plus >的例子來(lái)演示 c++ 友元類

其中 Remote 為 Tv的友元類。

Tv.h

#ifndef TV_H_
#define TV_H_

/*一個(gè)類 電視 */
class Tv
{
public:
  friend class Remote; //Remote類可以訪問(wèn)Tv Privite 的私有部分
  enum {
    off,on  //開(kāi)關(guān) 
  };
  enum 
  {
    MinVal,MaxVal=20  //音量
  };
  enum {
    Antena,Cable //使用的天線、還是電纜
  };
  enum 
  {
    TV ,DVD  //工作模式
  };

  Tv(int s = off, int mc = 125) :state(s), volume(5), maxchannel(mc),
    channel(5), mode(Cable), input(TV) {}
  void onoff() { state = (state == on) ? off : on; }
  bool ison()const { return state == on; }
  bool volup();  //增大聲音
  bool voldown(); //減小聲音
  void chanup(); //頻道 +
  void chandown();//頻道 -
  void set_mode() { mode = (mode == Antena) ? Cable : Antena; }
  void set_input() { input = (input == TV) ? DVD : TV; }
  void settings()const; //顯示所有設(shè)置


private:
  int state;  // 開(kāi)或者 關(guān)
  int volume; // 音量
  int maxchannel; //最大
  int channel;  //當(dāng)前頻道
  int mode;  // 廣播還是 電纜
  int input; //Tv 或者 DVD
};

/*Remote 的定義 (遙控器) */
class Remote {
private :
  int mode; // 控制 TV 或 DVD
public:
  Remote(int m = Tv::TV) :mode(m) {}
  bool volup(Tv & t) { return t.volup(); }
  bool voldown(Tv & t) { return t.voldown(); }
  void onoff(Tv & t) { return t.onoff(); }
  void chanup(Tv & t) { return t.chanup(); }
  void chandown(Tv & t) { return t.chandown(); }
  void set_chan(Tv &t, int c) { t.channel = c; } //訪問(wèn)了Tv的私有成員
  void set_mode(Tv &t) { t.set_mode(); }
  void set_input(Tv &t) { t.set_input(); }
};
#endif // TV_H_

Tv.cpp

#include "stdafx.h"
#include "Tv.h"
#include <iostream>

bool Tv::volup() {
  if (volume < MaxVal) {
    volume++;
    return true;
  }
  else {
    return false;
  }
}

bool Tv::voldown() {
  if (volume > MinVal) {
    volume--;
    return true;
  }
  else {
    return false;
  }
}

void Tv::chanup() {
  if (channel < maxchannel) channel++;
  else channel = 1;
}

void Tv::chandown() {
  if (channel > 1) channel--;
  else channel = maxchannel;
}

void Tv::settings() const {
  using std::cout;
  using std::endl;
  cout << "TV is " << (state == off ? "off" : "on") << endl;
  if (state == on) {
    cout << "Volume setting =" << volume << endl;
    cout << "Channel setting = " << channel << endl;
    cout << "Mode = " << (mode == Antena ? "antenna" : "cable") << endl;
    cout << "Input = " << (input == TV ? "TV" : "DVD") << endl;
  }
}

測(cè)試代碼:

#include "stdafx.h"
#include "tv.h"
#include <iostream>

int main()
{
  using std::cout;
  Tv s42;
  cout << "Initial settings for 42 \" Tv: \n";
  s42.settings();
  s42.onoff();
  s42.chanup();

  cout << " \n Adjusted settings for 42 \" Tv: \n";
  s42.chanup();
  cout << "\n Adjusted settings for 42 \" Tv: \n";
  s42.settings();

  Remote grey;
  grey.set_chan(s42, 10);
  grey.volup(s42);
  grey.volup(s42);
  cout << " \n s42 \" settings after using remote: \n";
  s42.settings();

  Tv s58(Tv::on);
  s58.set_mode();
  grey.set_chan(s58, 58);
  cout << " \n s58 \" setting: \n";
  s58.settings();

  system("pause");
  return 0;
}

運(yùn)行結(jié)果:

Initial settings for 42 " Tv:
TV is off

 Adjusted settings for 42 " Tv:

 Adjusted settings for 42 " Tv:
TV is on
Volume setting =5
Channel setting = 7
Mode = cable
Input = TV

 s42 " settings after using remote:
TV is on
Volume setting =7
Channel setting = 10
Mode = cable
Input = TV

 s58 " setting:
TV is on
Volume setting =5
Channel setting = 58
Mode = antenna
Input = TV
請(qǐng)按任意鍵繼續(xù). . .

上述代碼中將Remote類設(shè)置成為了Tv類的友元類,但事實(shí)上我們看到:唯一訪問(wèn)Tv的成員的方法是void set_chan(Tv &t, int c) { t.channel = c; } ,因此它是唯一需要友元的方法。因此不必讓整個(gè)類成為友元,這就引出了我們下面要講的的友元成員函數(shù)。

二、友元成員函數(shù)

我們要再Tv中將Remote::set_chan()設(shè)置成友元:

clas Tv
{
  friend void Remote::set_chan(Tv & t,int c ) ;
}

然而要使編譯器能夠處理這條語(yǔ)句,它必須知道Remote的定義。否則,它無(wú)法知道Remote是一個(gè)類。而 set_chan是這個(gè)類的方法。這意味著應(yīng)將Remote的定義放到Tv的定義前面。Remote的方法提到了Tv對(duì)象,而意味著Tv定義應(yīng)當(dāng)位于Remote定義之前,避開(kāi)這種循環(huán)依賴的方法是,使用前向聲明。
所以應(yīng)該這樣:

class Tv ; //前向聲明
class Remote{...}
class Tv {...}

這里還有一個(gè)麻煩就是:

Remote 包含了內(nèi)聯(lián)代碼例如:void onoff(Tv &t) {t.onoff();};

由于這將調(diào)用Tv的一個(gè)方法,所以編譯器此時(shí)已經(jīng)看到了Tv類的聲明,這樣才能知道Tv有哪些方法,但正如看到的,該聲明位于Remote聲明的后面。這種問(wèn)題的解決方法是:使用Remote聲明中只包含方法聲明,并將實(shí)際的定義放到Tv類之后。 所以最終應(yīng)該這樣:

class Tv; //前向聲明
class Remote {...} //如要用到Tv 只能是方法聲明
class Tv{...}
//接著寫Remote的定義

這里通過(guò)方法定義中使用 inline關(guān)鍵字,仍然可以使方法稱為內(nèi)聯(lián)方法
所以程序最終將tv.h改為:

#ifndef TV_H_
#define TV_H_
class Tv; //前向聲明
class Remote {

public:
  enum {
    off, on  //開(kāi)關(guān) 
  };
  enum
  {
    MinVal, MaxVal = 20  //音量
  };
  enum {
    Antena, Cable //使用的天線、還是電纜
  };
  enum
  {
    TV, DVD  //工作模式
  };

private:
  int mode; // 控制 TV 或 DVD
public:
  Remote(int m = TV) :mode(m) {}
  //用到了Tv 只能是聲明
  bool volup(Tv & t);
  bool voldown(Tv & t);
  void onoff(Tv & t);
  void chanup(Tv & t);
  void chandown(Tv & t);
  void set_chan(Tv &t, int c);
  void set_mode(Tv &t);
  void set_input(Tv &t);
};

class Tv
{
public:
  friend void Remote::set_chan(Tv & t,int c); //友元成員函數(shù)
  enum {
    off, on  //開(kāi)關(guān) 
  };
  enum
  {
    MinVal, MaxVal = 20  //音量
  };
  enum {
    Antena, Cable //使用的天線、還是電纜
  };
  enum
  {
    TV, DVD  //工作模式
  };
  Tv(int s = off, int mc = 125) :state(s), volume(5), maxchannel(mc),
    channel(5), mode(Cable), input(TV) {}
  void onoff() { state = (state == on) ? off : on; }
  bool ison()const { return state == on; }
  bool volup();  //增大聲音
  bool voldown(); //減小聲音
  void chanup(); //頻道 +
  void chandown();//頻道 -
  void set_mode() { mode = (mode == Antena) ? Cable : Antena; }
  void set_input() { input = (input == TV) ? DVD : TV; }
  void settings()const; //顯示所有設(shè)置
private:
  int state;  // 開(kāi)或者 關(guān)
  int volume; // 音量
  int maxchannel; //最大
  int channel;  //當(dāng)前頻道
  int mode;  // 廣播還是 電纜
  int input; //Tv 或者 DVD
};

inline bool Remote::volup(Tv & t) { return t.volup(); }
inline bool Remote::voldown(Tv & t) { return t.voldown(); }
inline void Remote::onoff(Tv & t) { return t.onoff(); }
inline void Remote::chanup(Tv & t) { return t.chanup(); }
inline void Remote::chandown(Tv & t) { return t.chandown(); }
inline void Remote::set_chan(Tv &t, int c) { t.channel = c; }
inline void Remote::set_mode(Tv &t) { return t.set_mode(); }
inline void Remote::set_input(Tv &t) { return t.set_input(); }
#endif // TV_H_

測(cè)試結(jié)果不變。

*另外:也可一個(gè)將內(nèi)聯(lián)函數(shù)放在tv.cpp中,但必須去掉inline關(guān)鍵字,這樣函數(shù)的連接性將成為外部的。

三、其他友元關(guān)系

1、上面的代碼表示的是Remote是Tv的友元。但我們有時(shí)也會(huì)用到2個(gè)類互相友元。即Remote是Tv的友元,同時(shí) Tv又是Remote的友元

他們定義與下面類似:

class Remote
class Tv
{
friend clas Remote
public:
  void buzz(Remote & r) ;
  ...
}

class Remote
{
friend class Tv;
public:
  void Bool volup(Tv & t){t.volup();}
  ...
}
inline void Tv::buzz(Remote & r)
{
...
}

由于Remote的聲明位于Tv聲明的后面,所以可以在類的定義Remote::volup(),但Tv::buzz()方法必須在Tv聲明的外部定義,使其位于Remote聲明的外面。如果不希望buzz()是內(nèi)聯(lián)的,則應(yīng)在一個(gè)單獨(dú)的方法定義文件中定義它。

2、共同的友元。

需要使用友元的另一種情況是,函數(shù)需要訪問(wèn)兩個(gè)類的私有數(shù)據(jù)。它可以是一個(gè)類的友元,同時(shí)是另一個(gè)類的友元。示例如下:

class Analyzer;
class Probe
{
  friend void sync (Analyzer & a,const Probe & p) ;
  friend void sync (Probe & p,const Analyzer & a);
  ...
};
class Analyzer
{
  friend void sync (Analyzer & a,const Probe & p) ;
  friend void sync (Probe & p,const Analyzer & a);
}
inline void sync (Analyzer & a,const Probe & p)
{
  ...
}
inline void sync (Probe & p,const Analyzer & a)
{
  ...
}

關(guān)于“C++中友元的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

分享題目:C++中友元的示例分析
分享URL:http://muchs.cn/article24/ghioje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、、軟件開(kāi)發(fā)、品牌網(wǎng)站制作、標(biāo)簽優(yōu)化、全網(wǎng)營(yíng)銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)