Reputation: 49
I know friend classes and I want exactly opposite of it.
Ex: Class A has a public function named F. While other 30 classes have access to F function, Class Z cannot access to F function.
Upvotes: 1
Views: 937
Reputation: 1
As mentioned in the other answers, there's no way to kind of unfriend
or exclude
a particular class from your public interface.
Though you can use the same principle I to have protected
interfaces, as have described in this Q&A:
How can I remove/refactor a «friend» dependency declaration properly?.
This would require a client to implement a specific interface to call a function from the class that provides the protected
interface:
Here's the C++ implementation:
class ClassAAccessor { public: ClassAAccessor(ClassA& classA); void setInternalInterfaceRef(InternalInterface & newValue) { internalInterfaceRef = &newValue; } private: InternalInterface* internalInterfaceRef; };
This one is actually called, when the also newly introduced method
ClassA::attachAccessor()
method is called:class ClassA : protected InternalInterface { public: // ... attachAccessor(ClassAAccessor & accessor); // ... }; ClassA::attachAccessor(ClassAAccessor & accessor) { accessor.setInternalInterfaceRef(*this); // The internal interface can be handed // out here only, since it's inherited // in the protected scope. }
Thus the constructor of
ClassAAccessor
can be rewritten in the following way:ClassAAccessor::ClassAAccessor(ClassA& classA) : internalInterfaceRef(0) { classA.attachAccessor(*this); }
I don't want to mark your question as duplicate, because the answer I link was more about UML design, but contains a c++ sample.
Upvotes: 1
Reputation: 1977
Off the top of my head, I don't think that's possible. You want Z calls to A::F to generate a compile error? I'm not sure how you would even add that feature to a language. If such a feature did exist, then either you would have to create a list in Class A which contains all of the targets which can or cannot use function F, or you would need to specify in other classes that they are not allowing themselves to use A::F.
So you want something like
class Z {
denied A::F;
}
That would probably be the best/easiest way to add such a feature, with some keyword meant to be used like above.
I don't think it exists. Features like friend exist because it is often very useful to add accessibility to something which normally you could not access. It is rarely necessary to specifically remove accessibility to something you normally can access, and the answer from compiler engineers would probably be "Just don't use A::F in Z."
Not saying the idea is dumb or that it's useless, just that it's not useful enough to be a feature, which is why I don't think it is one.
Upvotes: 0
Reputation: 31459
If the function is public you cannot prevent others from calling it.
You can make it so that the function is not exported by the linker outside the library where it is implemented, but that's about it.
What problem are you actually trying to solve?
Upvotes: 4