Possible Duplicate:
clean C++ granular friend equivalent? (Answer: Attorney-Client Idiom)
I've wanted this a couple times and haven't been able to come up with a decent way to do it.
Say I've got a member function in class A. I want to be able to call that function from an unrelated class B, but not be generally callable. You might say, "Sure, make the function private and declare B to be a friend of A." That's what I've been doing, but it seems a bit overkill. I don't really want to give B access to everything in A, just the one function.
In short: A::func()
callable only by class B, but B not declared a friend of A. Possible?
Private: The class members declared as private can be accessed only by the member functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of the class.
Yes, it is possible using pointers. Although it's a loophole in C++, yes it's possible through pointers.
Yes, we can write member function of one class as friend of another class. These can be one of the important uses/needs of friend function in c++.
Accessing Private Data Members Outside Class is Not Possible in C++ unless a friend function is used or is accessed by a member function. So, there's no question of accessing them outside a program.
You could split the interface of A
into several pure abstract base classes and then give B
a reference to the interface that has the appropriate method. Other classes would only get interfaces that do not contain this method. Keep in mind that this is not very scalable, as the number of interfaces can quickly become very large.
One possible way might be to create a trusted class that wraps A::func
and pass a wrapper object to B
. This again requires the wrapper to be a friend of A
, but you only need to manage one such class, while all external classes can make use of the wrapper.
Example:
class Wrapper;
class A {
private:
void func();
/* other methods */
public:
Wrapper getWrapper();
friend class Wrapper;
};
class Wrapper {
private:
A &ref;
private:
Wrapper(A &obj) : ref(obj) { }
public:
void func() {
ref.func();
}
friend class A;
};
Wrapper A::getWrapper() {
return Wrapper(*this);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With