Path: utzoo!attcan!uunet!husc6!bbn!bbn.com!lpringle From: lpringle@bbn.com (Lewis Pringle) Newsgroups: comp.lang.c++ Subject: Re: Friend specifier considered harmful Message-ID: <29433@bbn.COM> Date: 8 Sep 88 02:28:37 GMT References: <61@cybaswan.UUCP> <8160@alice.UUCP> Sender: news@bbn.COM Reply-To: lpringle@BBN.COM (Lewis Pringle) Organization: Bolt Beranek and Newman Inc., Cambridge MA Lines: 44 In article <8160@alice.UUCP> ark@alice.UUCP (Andrew Koenig) writes: > >For example, a linear algebra package might have Vector and Matrix >classes. Although some people might claim that a vector is >just a 1xN matrix, others might prefer an Nx1 matrix, and >therefore I suggest that vectors and matrices are really >different beasts and neither is a species of the other. > >Now, how do you write a function to multiply a Matrix by a >Vector? It apparently needs to know about the internal >representations of both classes. Moreover, symmetry suggests >that it should be a member of both or neither -- which >perforce means neither. Friends are the only way to make >this possible. I am not sure that your example well illustrates your point, since accessing the size, and elements of both vectors and matrices would be public methods of those classes. I think thats really all you need to implement multiplication, even if you are efficiency hacking (in which case inline functions as accessors to the matrices would be a good choice). I am not sure if I like the use of friends, but I can share a place where I used them, and could not think of a better way. I was implementing an object-oriented portable windowing system, and I defined high level window object (class). It had pointers to implementation specific structures (e.g. the OS Window pointer) which needed to be referenced by another class (the Controls). I could have made a public accessor for this machine-dependent information, but I wanted the public interface to be entirely portable, and the use of friends was the only way I could find to acheive that. It seems to me that the trouble is that C++ provides a restrictive form of data hiding (not that I know of a better way!): its too "all or nothing". The use of friends is not exacly what you want. Perhaps if in specifying a friend,you didn't give blanket access to the given class, but instead to just an explicitly stated set of members? Lewis. "OS/2: half an operating system for half a computer." In Real Life: Lewis Gordon Pringle Jr. Electronic Mail: lpringle@labs-n.bbn.com Phone: (617) 873-4433