Becoming friend-ly
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.
4. You can find a good discussion of this at www.cprogramming.com/tutorial/friends.html. Scott Meyers infers something similar when he says, “It ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access