A relatively small number of languages have the equivalent of the C++
friend keyword. For example, Microsoft C# has the
InternalsVisibleToAttribute class that can be used to grant access from specified contexts.
While many consider the
friend keyword to be “dirty programming” and to violate principles of encapsulation,4 it is often cited as having positive value for testing purposes, especially unit testing. Consider a C++ implementation of a poker hand (Listing 9-10), in this case in a simple game of five-card stud. Of course, you do not want to reveal your cards to anyone else.