tags:

views:

58

answers:

1

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?

Thanks

Some kind of example:

class A {
//stuff
};
class B {
int r; //A cant use this
MagicFriendKeyword A void func(); //A can use this
public:

};
+1  A: 

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

ArunSaha
I just want A to be able to call 1 private function of B.
Milo
I want all of class A to access one function of class B since hundreds of functions in A will use it.
Milo
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
@JoshD not really, I want to grant all of A access to one function of B which is private
Milo
@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
I'm not sure I understand their example, which part exactly demonstrates it?
Milo
@ James McNellis see my edit
Milo
@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
@All: Apologies. I answered the question at its 1st revision, and interpreting only from the text without the pseudo-code, I mis-interpreted it.
ArunSaha