Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!mcsun!ub4b!kulcs!herman From: herman@kulcs.uucp (Herman Moons) Newsgroups: comp.lang.c++ Subject: are 'friend's really necessary ?? Message-ID: <169@pollux.kulcs.uucp> Date: 16 Mar 90 16:23:52 GMT Reply-To: herman@kulcs.UUCP () Organization: Katholieke Universiteit Leuven, Dept. Computer Science Lines: 51 I don't know whether this question appeared earlier on the net. Are there any *good* reasons for having the friend concept in C++ ? The example given in Lippman's book: class Screen; Screen myScreen; cout << myScreen; // friend needed to implement this does not seem convincing. The friend mechanism is used in this case to circumvent a bad design choice. After all, << is an operator defined in the stream classes, while outputting an object logically belongs to the object itself (you say to the object: output yourself). Wouldn't it be better to say: class Screen { ostream& operator>>= (ostream& out); // no friend needed here }; myScreen >>= cout; yourScreen >>= myScreen >>= cout; Discussions with my colleagues didn't provide an example were friends were really necessary. We could always find ways to implement our classes such that the information hiding principle wasn't violated. We also couldn't find a good example where data abstraction resulted in a serious loss of efficiency (we could always find sensible inline functions that provided fast access to the needed information). So, if you have an example that really proves the necessity of the friend mechanism, please post it. After all, if there is no real necessity, the mechanism is just an unnessary loophole in the language to defeat the information hiding principle. ----------------------------------------------------------------------------- Herman Moons Katholieke Universiteit Leuven Dept. of Computer Science Tf : +32 (16) 20.06.56 Celestijnenlaan 200A Fax: +32 (16) 20.53.08 B-3030 Heverlee-Leuven Belgium e-mail: herman@kulcs.uucp herman@blekul60.bitnet herman@cs.kuleuven.ac.be -----------------------------------------------------------------------------