久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

什么時(shí)候應(yīng)該在 C++ 中使用“朋友"?

When should you use #39;friend#39; in C++?(什么時(shí)候應(yīng)該在 C++ 中使用“朋友?)
本文介紹了什么時(shí)候應(yīng)該在 C++ 中使用“朋友"?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我一直在閱讀 C++ FAQ 并且對 friend 聲明.我個(gè)人從未使用過它,但我對探索該語言很感興趣.

I have been reading through the C++ FAQ and was curious about the friend declaration. I personally have never used it, however I am interested in exploring the language.

使用 friend 的好例子是什么?

What is a good example of using friend?

閱讀 FAQ 多一點(diǎn)我喜歡 << >> 運(yùn)算符重載和添加為這些類的朋友的想法.但是我不確定這不會破壞封裝.這些異常什么時(shí)候才能保持在 OOP 的嚴(yán)格范圍內(nèi)?

Reading the FAQ a bit longer I like the idea of the << >> operator overloading and adding as a friend of those classes. However I am not sure how this doesn't break encapsulation. When can these exceptions stay within the strictness that is OOP?

推薦答案

首先 (IMO) 不要聽那些說 friend 沒有用的人.它是有益的.在許多情況下,您將擁有不打算公開提供的數(shù)據(jù)或功能的對象.對于具有許多作者的大型代碼庫尤其如此,他們可能只是表面上熟悉不同領(lǐng)域.

Firstly (IMO) don't listen to people who say friend is not useful. It IS useful. In many situations you will have objects with data or functionality that are not intended to be publicly available. This is particularly true of large codebases with many authors who may only be superficially familiar with different areas.

友元說明符有多種替代方案,但它們通常很麻煩(cpp 級別的具體類/屏蔽類型定義)或不是萬無一失的(注釋或函數(shù)名稱約定).

There ARE alternatives to the friend specifier, but often they are cumbersome (cpp-level concrete classes/masked typedefs) or not foolproof (comments or function name conventions).

進(jìn)入答案;

friend 說明符允許指定的類訪問受保護(hù)的數(shù)據(jù)或在發(fā)出友元語句的類中的功能.例如在下面的代碼中,任何人都可以向孩子詢問他們的名字,但只有母親和孩子可以更改名字.

The friend specifier allows the designated class access to protected data or functionality within the class making the friend statement. For example in the below code anyone may ask a child for their name, but only the mother and the child may change the name.

您可以通過考慮更復(fù)雜的類(例如 Window)來進(jìn)一步了解這個(gè)簡單的示例.一個(gè) Window 很可能有許多不應(yīng)公開訪問的函數(shù)/數(shù)據(jù)元素,但相關(guān)類(例如 WindowManager)需要這些元素.

You can take this simple example further by considering a more complex class such as a Window. Quite likely a Window will have many function/data elements that should not be publicly accessible, but ARE needed by a related class such as a WindowManager.

class Child
{
//Mother class members can access the private parts of class Child.
friend class Mother;

public:

  string name( void );

protected:

  void setName( string newName );
};

這篇關(guān)于什么時(shí)候應(yīng)該在 C++ 中使用“朋友"?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉(zhuǎn)換為 HSV 并將 HSV 轉(zhuǎn)換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉(zhuǎn)換為字符串?)
When to use inline function and when not to use it?(什么時(shí)候使用內(nèi)聯(lián)函數(shù),什么時(shí)候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關(guān)嗎?)
主站蜘蛛池模板: 亚洲精品一区二区在线观看 | 国产一区二区三区四区 | 高清无码| 东北少妇bbbb搡bbb搡 | 黄色一级在线观看 | a毛片视频| 在线日韩 | 成人在线免费网站 | 日本加勒比视频 | 日本不卡在线视频 | 日韩在线视频观看 | 国产午夜精品福利 | 精品视频在线观看免费 | 日韩欧美中文字幕在线观看 | 欧美另类视频 | 黄色片视频免费 | 日韩精品一区在线 | 国产精品777 | 黄色av免费观看 | 成年网站在线观看 | 中文字幕第一 | 国产激情一区二区三区 | 日本激情网| 免费一级大片 | 天天插天天插 | 亚洲成人精品视频 | 日韩在线不卡视频 | 婷婷在线视频 | 色综合五月天 | 国产成人午夜 | 黄色小视频在线免费观看 | 亚洲精品中文字幕乱码三区91 | 欧美午夜视频 | 小日子的在线观看免费第8集 | 成人在线小视频 | 天天操天| 欧美日韩四区 | 欧美黄色网 | 日韩一级视频 | 玖玖精品视频 | 日韩免费视频 |