Although class friendship is one of the latest C ++ resorts, does this template make sense?
class Peer
{
public:
friend class Peer;
void GetSecret(const Peer& other)
{
const std::string& secret = other.GiveSecret();
std::cout << secret << std::endl;
}
private:
const std::string& GiveSecret() const
{
return "secrety stuff";
}
};
int main(int argc, char* argv[])
{
Peer peerA;
Peer peerB;
peerA.GetSecret(peerB);
return 0;
}
Well, the reason for this pattern is that Peers have the same rank and they need to share knowledge with each other, but this knowledge is secret, because no one except peers should use it, or the program is no longer valid.
One very real example: when one peer instance is created from another peer, it needs to access secret information from its original partner, but again there is no reason for anyone else to know about these internal components, just peers.
source
share