久久久久久久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)嗎?)
主站蜘蛛池模板: 免费毛片视频 | 国产精品视频99 | 国产欧美日韩综合 | 日韩毛片网 | 九九精品在线观看 | 日韩中文字幕精品 | 亚洲欧美在线观看视频 | 亚洲精品无 | 国产一区二区在线观看视频 | 天天干天天做 | 亚洲一区二区在线视频 | 蜜桃色一区二区三区 | 国产精品一级二级 | 拍床戏真做h文黄肉1v1 | 少妇高潮久久久久久潘金莲 | 日韩三级精品 | 亚洲高清在线 | 欧美黄色片在线观看 | 91日韩在线 | 欧美a在线观看 | 91国在线 | 懂色av蜜臀av粉嫩av分享 | 高潮毛片无遮挡免费看 | 国模一区二区 | 天天综合av | 欧美黄色一级大片 | 毛片网站在线播放 | 国产做受入口竹菊 | 国产成人在线播放 | 九九热只有精品 | 国产精品一区三区 | 欧美日韩精品一区二区 | 中文字幕免费在线观看 | 精品日韩一区二区三区 | 亚洲国产免费 | 私人午夜影院 | 男人添女荫道口图片 | 日本成人一区二区三区 | www.色偷偷 | 三级福利视频 | 亚洲一级片在线观看 |