Topic: Friend region


Author: Tom Bjorkholm <Tom.Bjorkholm@ericsson.com>
Date: Tue, 18 Dec 2001 16:17:14 GMT
Raw View
I sometimes find the friend keyword blunt. Every now
and then there is a class with one single method that
should be callable from only one other function/class.
This is somewhat related to Eric Desrosiers' "partial friend",
but I propose a totally different approach.


Consider this class as designed today:

class A
{
public:
   int foo();
   void bar();
private:
   A(); // not allowed, not implemented
   friend int main(int, char**); // only main can construct A
   A(int argc, char** argv); //only called by main
   int x,y,z;
};

Here one member function (in this case the constructor) is
only callable from one function (in this case main()).
But the code opens up so that main() can directly access
the member variables, which might be a maintenance
nightmare if this is in a library.

Wouldn't it be good if friend declarations affected only
a region of the class declaration, in the same way as we
have public and private regions. Then we could write:

class A
{
public:
   int foo();
   void bar();
private:
   A(); // not allowed, not implemented
   int x,y,z;
friend int main(int, char**): // start of friend region
   A(int argc, char** argv); // only called by main
};


/Tom


--
--------------------------------------------------------------------
Tom Bjorkholm  * Ericsson Enterprise AB   * tom.bjorkholm@ericsson.com
NA/EBC/PEMS/UL * Nacka Strand             * Tel: +46-8-422 1348
               * 13 189 Stockholm, Sweden * Fax: +46-8-422 1010

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]