Path: utzoo!utgpu!attcan!uunet!lll-winken!lll-tis!ames!ncar!gatech!rutgers!bellcore!faline!thumper!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: Friend specifier considered harmful Message-ID: <8160@alice.UUCP> Date: 2 Sep 88 14:25:34 GMT References: <61@cybaswan.UUCP> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 35 In article <61@cybaswan.UUCP>, eeartym@cybaswan.UUCP (Dr R.Artym eleceng ) writes: > Do YOU have a c++ program that couldn't have been written without > the friend specifier? Obviously not -- I can write a universal Turing machine simulator without the friend specifier. But the real question is: Are there problems for which friends are the ``best'' solution? My answer is YES. Friends are useful any time you want to have several interrelated classes that can't be naturally described by inheritance. 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. If you are tempted to respond that this just shows that matrices and vectors should either be the same class or one should inherit from the other, my response is that my argument still holds for any two classes that need to be linked by some means other than inheritance -- you won't convince me that friends are unnecessary until you have convinced me that no such classes can ever exist. -- --Andrew Koenig ark@europa.att.com