比如這個班。有沒有可能讓非會員功能執行好友功能的任務?一個非成員函數如何實現一個類的朋友函數呢?
class Accumulator
{
private:
int m_nValue;
public:
Accumulator() { m_nValue = 0; }
void Add(int nValue) { m_nValue += nValue; }
// Make the Reset() function a friend of this class
friend void Reset(Accumulator &cAccumulator);
};
// Reset() is now a friend of the Accumulator class
void Reset(Accumulator &cAccumulator)
{
// And can access the private data of Accumulator objects
cAccumulator.m_nValue = 0;
}
朋友被定義爲非會員... – 2014-09-21 03:25:38
私人的想法是,非會員非朋友無法訪問它。 – Dani 2014-09-21 03:33:13