Czytam o funkcjach zaprzyjaźnionych i o ile idea wydaje sie być prosta, o tyle nie rozumiem żadnego z przykładów.
Pierwszy:
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;
}
Drugi (fragment):
class Zakres {
int lewy, prawy;
friend void isInside(const Punkt*, const Zakres*);
public:
Zakres(int lewy = 0, int prawy = 0)
: lewy(lewy), prawy(prawy)
{ }
};
Trzeci:
class K {
friend void fun( K& ); // deklaracja przyjaźni
friend void Y::fun( K& ); // deklaracja przyjaźni funkcji składowej klasy 'Y'
private:
int x;
};
void fun( K& obiekt ) {
obiekt.x = 12; // nie byłoby możliwe to przypisanie bez określenia przyjaźni
}
Nie rozumiem dlaczego w pierwszym piszemy ze znakiem & np. &cAccumulator, w drugim jest Punkt*, a w trzecim jeszcze inaczej bo K&. Co oznaczają te zapisy? Proszę o pomoc.