basti@orthogo.UUCP (Sebastian Wangnick) (10/12/90)
I understood from my C++ book that friends have the same
access rights as member functions. Now I have some questions
on this:
Friends must be allowed to access member variables of
all their friend class' objects, whereas member functions
should only be allowed to access their own member variables?
My compiler allows member functions to access member variables
of other objects with the same class, too!?
More problematically, shouldn't friends be allowed to access
protected: base class members the same way as members can?
Look at the following code fragment:
class Base { protected: int i; };
class D : public Base
{
int Get1 () { return i; }
int Get2 (D d) { return d.i; }
friend int Get3 (D d) { return d.i; }
};
In my opinion, Get2 should be illegal (accessing hidden member
of other object), whereas Get3 should be allowed to do so.
But my compiler (Glockenspiel Domain/C++, cfront derivate, 1.2)
allows Get2 and flags Get3 with:
error: d is protected
What is your opinion about that, netters?
Sebastian Wangnick (basti@orthogo.uucp)
jac@sundance.llnl.gov (James Crotinger) (10/13/90)
In article <811@orthogo.UUCP> basti@orthogo.UUCP (Sebastian Wangnick) writes: >My compiler allows member functions to access member variables >of other objects with the same class, too!? > >More problematically, shouldn't friends be allowed to access >protected: base class members the same way as members can? > I think the first idea, that member functions should only be able to access the private members belonging to *this, would be much too severe a restriction. Objects of the same class by definition know about eachother's representations and should be able to use that knowledge to their advantage, for efficiency reasons, etc. If you want to make sure that an one object doesn't missuse that knowledge, prototype the member functions accordingly (to take const references, etc.). The second point is, I think, a bug in 1.2. Was this fixed in 2.0? > >Sebastian Wangnick (basti@orthogo.uucp) Jim -- ----------------------------------------------------------------------------- James A. Crotinger Lawrence Livermore Nat'l Lab // The above views jac@gandalf.llnl.gov P.O. Box 808; L-630 \\ // are mine and are not (415) 422-0259 Livermore CA 94550 \\/ necessarily those of LLNL.