久久久久久久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)嗎?)
主站蜘蛛池模板: 国产精彩视频在线观看 | 色影视 | 一区二区三区视频在线免费观看 | 成年无码av片在线 | 91精品久久久久久久久久 | 欧美另类视频 | 欧美日韩精品一区二区三区四区 | 在线激情视频 | 色性av| 亚洲精品一区二区在线观看 | 中文字幕 在线观看 | 西西裸体做爰视频 | 日本一二三区在线观看 | 欧美激情视频一区二区三区在线播放 | 久久综合久色欧美综合狠狠 | 国产网站在线 | www.天天干.com | www日本高清 | 国产日韩一区二区三区 | 久久一区二区三区免费 | av黄色在线观看 | 日韩中文字幕第一页 | 国产一级片一区二区 | 丝袜美腿一区二区三区 | 91欧美| av黄色免费在线观看 | 日本成人午夜影院 | 日韩av一区二区在线观看 | 91精品国产一区二区在线观看 | 午夜不卡福利视频 | 日韩一级黄色毛片 | 久久1区| 成人免费视频在线观看 | 日韩精品视频在线观看一区二区三区 | 97超碰站 | 亚洲三级在线观看 | 午夜精品一区 | 天天操天天干天天爽 | 亚洲精品一区二区冲田杏梨 | 草草视频在线观看 | 手机在线观看av |