Is there a way to create friendship between specific objects rather than between classes? Take a look at this simple, short example of class friendship, after which I will pose my question:
#include <string>
#include <iostream>
using std::cout;
using std::string;
using std::endl;
class Dog;
class Man {
public:
friend class Dog;
Man(string shoe) {shoe_ = shoe;}
private:
string shoe_;
};
class Dog {
public:
string Fetch(Man man) {return man.shoe_;}
};
int main() {
Man Joe("Nike");
Dog Fido;
cout << Dog.Fetch(Joe) << endl;
return 0;
}
This example makes Dog a Friend class of Man. But this would mean that every dog is every man's friend. What if I wanted to only have Fido as Joe's friend? This way, if I had another Dog, defined as
Dog Puddles;
and I don't want Puddles to access Joe's shoe_, but want Fido to, how can I do this?
I do believe it is practically important. Dog fetching shoes, maybe not; but that was intended as a simplification that would be easy to understand. But think of Man as being a client of a financial firm, say KPMG; shoe_ being his financial portfolio; and Dog a portfolio analysis object tool belonging to financial analyst X. X has a client list, and he is authorized to look at the private information of just his clients, but not all clients of KPMG (which is why I said "specific objects" in my question). How does he do it?