Appropriate use of friend? Container class designed to manipulate objects of specific type
Lets say you have a FooManager
made to manage multiple objects of type Foo
. The FooManager
needs to see some parts of its Foo
s to evaluate their current state. Before I was using a few accessors in Foo
to see these parts, until I realized that FooManager
is the only class actually using these. I decided to m开发者_Go百科ake FooManager
a friend of Foo
. This resulted in most of class Foo
becoming private.
Is this an appropriate use of friend? My reasoning was that it helps encapsulation because while it gives FooManager
complete access to Foo
's internals, it completely blocks off access to everything else.
Unless there is a different and cleaner way to achieve what you're trying to achieve, this sounds like a fair approach.
With regards to encapsulation, see: http://www.parashift.com/c++-faq-lite/friends.html#faq-14.2.
精彩评论