Suppose I have three C++ classes FooA, FooB and FooC.
FooA has an member function named Hello
, I want to call this function in class FooB, but I don\'t
No, and this is not really a limitation. To my mind, the limitation is that friend
— a blunt weapon for hacking around design flaws — exists in the first place.
Your class FooA
has no business knowing about FooB
and FooC
and "which one should be able to use it". It should have a public interface, and not care who can use it. That's the point of the interface! Calling functions within that interface should always leave the FooA
in a nice, safe, happy, consistent state.
And if your concern is that you might accidentally use the FooA
interface from somewhere you didn't mean to, well, simply don't do that; C++ is not a language suited to protecting against these kinds of user errors. Your test coverage should suffice in this case.
Strictly speaking, I'm sure you can obtain the functionality you're after with some ghastly complicated "design pattern" but, honestly, I wouldn't bother.
If this is a problem for the semantics of your program's design, then I politely suggest that your design has a flaw.