Question on Friend Classes

Hi,

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?

Thanks,

Kumar
Hi,

I made a small error in the listing above. I meant to type

int main() {
Man Joe("Nike");
Dog Fido;
cout << Fido.Fetch(Joe) << endl; // not Dog.Fetch(Joe)
return 0;
}

Sorry for that, and thanks for your responses.

Kumar
Not possible in C++. (at least there's no built-in functionality that does this)

This doesn't have much (any?) practical application in programming anyway.
Ok.

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?

Thanks,

Kumar
That kind of thing can't be determined at compile time. You'd have to keep a list of clients that have access at runtime.

Like have some kind of password system or something.
Topic archived. No new replies allowed.