Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a friend class have only special access to 1 function of another class? [duplicate]

Tags:

c++

Possible Duplicate:
Is this key-oriented access-protection pattern a known idiom?

I have class A and class B. I want class A to access one of class B's private functions; but only that, not everything else. Is that possible?

Some kind of example:

class A {
  //stuff
};

class B {
  int r; // A cant use this
  MagicFriendKeyword A void func(); // A can use this
public:
  ...
};
like image 402
jmasterx Avatar asked Oct 14 '22 22:10

jmasterx


1 Answers

If there is one (or few) members functions in class A, that want to use class B's private member functions, then you can declare those one/few functions as friend. E.g.

class B {
    // ...
    friend void A::mutateB( B * );
    // ...
};

See http://en.wikipedia.org/wiki/Friend_function

like image 134
Arun Avatar answered Oct 25 '22 16:10

Arun