5

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:
  ...
};
Community
  • 1
  • 1
jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • 4
    Duplicate of [Is this key-oriented access-protection pattern a known idiom?](http://stackoverflow.com/questions/3220009/is-this-key-oriented-access-protection-pattern-a-known-idiom). That question gives an example of how you can achieve what you are asking; actually [this question](http://stackoverflow.com/questions/3217390/clean-c-granular-friend-equivalent-answer-attorney-client-idiom) might be a better duplicate. – James McNellis Oct 11 '10 at 02:30
  • Also see [this question](http://stackoverflow.com/questions/3324898/can-we-increase-the-re-usability-of-this-key-oriented-access-protection-pattern), which seeks to generalize it. I think we ended up with the name "passkey friend idiom". The idea is that only certain classes can create a "passkey" type, and the function only accepts certain passkeys, granting access or "friendship". – GManNickG Oct 11 '10 at 03:08
  • There are more -- and depending on context better -- solutions than covered by the referenced discussions. In particular, factoring out `func` as an interface. Don't close open-ended questions, please, unless the similar old discussion is just as open-ended and complete. – Cheers and hth. - Alf Oct 11 '10 at 03:41

1 Answers1

3

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

Arun
  • 19,750
  • 10
  • 51
  • 60
  • I just want A to be able to call 1 private function of B. – jmasterx Oct 11 '10 at 02:37
  • I want all of class A to access one function of class B since hundreds of functions in A will use it. – jmasterx Oct 11 '10 at 02:41
  • 9
    This isn't what's asked. This still gives that function access to _all_ members of B. He wants to grant access to a single member of B. – JoshD Oct 11 '10 at 02:42
  • @JoshD not really, I want to grant all of A access to one function of B which is private – jmasterx Oct 11 '10 at 02:43
  • @Milo: Does the idiom described in the questions to which I linked in a comment to the question adequately resolve your issue? If not, perhaps you can explain why so that people can better answer your question. – James McNellis Oct 11 '10 at 02:56
  • I'm not sure I understand their example, which part exactly demonstrates it? – jmasterx Oct 11 '10 at 02:59
  • @Milo: "Hundreds of functions in A will use it" you say? Having a single class with hundreds of functions is terrible, you might want to look at cleaning up that problem first. – Ben Voigt Oct 11 '10 at 03:20
  • @All: Apologies. I answered the question at its 1st revision, and interpreting only from the text without the pseudo-code, I mis-interpreted it. – Arun Oct 11 '10 at 03:25